题目大意:
给你给定个数的文件名,按字典序排序之后输出,如果输出是在最后一列,输出长度为输入长度的最大长度,否则输出长度为输入长度的最大长度+2。输出时一行最多只能输出60个字符。按列输出,第一列输完之后输第二列,一直到最后一列。
思路:输入存2维数组,用qsort按字典序排序度找出输入的最长字符串长度,计算每行最多能输入多少个字符串即输出时每行会有几列,再计算要输出多少行。最后按输出格式输出就可以了。
总结:输出格式有点麻烦,别的就没什么了!
代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> using namespace std; char str[110][70]; int cmp(const void *_a, const void *_b) { char *a = (char *)_a; char *b = (char *)_b; return strcmp(a, b); } int main() { int t; while (scanf("%d", &t) != EOF) { memset(str, 0, sizeof(str)); int len = 0; for (int i = 0; i < t; i++) { scanf("%s", str[i]); if (len < strlen(str[i])) len = strlen(str[i]); } for (int i = 0; i < t; i++) { for (int j = 0; j < len; j++) { if (str[i][j] == '') { for (int k = j; k < len; k++) str[i][k] = ' '; str[i][len] = ''; } } } for (int i = 0; i < 60; i++) printf("-"); printf("n"); qsort(str, t, sizeof(str[0]), cmp); int cl = 0; int l = len; while (l <= 60) { l = l + len + 2; cl ++; } int r = 0; if (t % cl == 0) r = t / cl; else r = t / cl + 1; for (int i = 0; i < r; i++) { for (int j = 0; j < cl - 1; j++) { printf("%s ", str[j * r + i]); } printf("%sn", str[r * (cl - 1) + i]); } } return 0; }
最后
以上就是忧伤镜子最近收集整理的关于UVA 400 - Unix ls的全部内容,更多相关UVA内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复