码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 400 (水题) Unix ls

时间:2015-01-07 21:52:04      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

题意:

有n个文件名,排序后按列优先左对齐输出。设最长的文件名的长度为M,则最后一列长度为M,其他列长度为M+2.

分析:

这道题很简单,但要把代码写的精炼,还是要好好考虑一下的。lrj的代码中有两个亮点,一个是print子函数,一个就是行数的计算。用心体会

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxl = 60;
 8 const int maxn = 100 + 5;
 9 string names[maxn];
10 
11 void print(const string& s, int len, char extra)
12 {
13     printf("%s", s.c_str());
14     for(int i = s.length(); i < len; ++i) putchar(extra);
15 }
16 
17 int main()
18 {
19     //freopen("in.txt", "r", stdin);
20     int n;
21     while(scanf("%d", &n) == 1)
22     {
23         int M = 0;
24         for(int i = 0; i < n; ++i)
25         {
26             cin >> names[i];
27             M = max(M, (int)names[i].length());
28         }
29         sort(names, names + n);
30         int cols = (maxl - M) / (M + 2) + 1, rows = (n - 1) / cols + 1;
31         print("", 60, -);
32         puts("");
33         for(int r = 0; r < rows; ++r)
34         {
35             for(int c = 0; c < cols; ++c)
36             {
37                 int index = c * rows + r;
38                 if(index < n) print(names[index], c == cols-1 ? M : M+2,  );
39             }
40             puts("");
41         }
42     }
43 
44     return 0;
45 }
代码君

 

UVa 400 (水题) Unix ls

标签:

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4209281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!