我是靠谱客的博主 寒冷唇膏,这篇文章主要介绍UVa 400 - Unix ls解题报告,现在分享给大家,希望可以做个参考。

一道简单的字符串排序问题,只是输出格式要注意。找到规律就不难了。


复制代码
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
//400 - Unix ls #include <iostream> #include <cstring> #include <cstdlib> using namespace std; int cmp_words(const void *_a, const void *_b) { char *a = (char*)_a; char *b = (char*)_b; return strcmp(a, b); } char words[110][100]; void space(int, int); int main() { //freopen("data.txt", "r", stdin); //freopen("output.txt", "w", stdout); int n; while (scanf("%d", &n) != EOF) { memset(words, 0, sizeof(words)); getchar(); for(int i = 0; i < n; i++) scanf("%s", words[i]); qsort(words, n, sizeof(words[0]), cmp_words);//排序 int maxlen = strlen(words[0]); for(int i = 1; i < n; i++)//找到最大长度 if(maxlen < strlen(words[i])) maxlen = strlen(words[i]); int col = 62 / (maxlen + 2);//这里之前判断条件写错,有漏洞,导致wrong。 int row = (n - 1) / col + 1; for(int i = 0; i < 60; i++) printf("-"); printf("n"); for(int i = 0; i < row; i++)//按要求输出 { for(int j = 0; j < col && (i + row * j) < n; j++) { printf("%s", words[i + row * j]); /*if(j != col - 1)*/ space(strlen(words[i + row * j]), maxlen);//这里最后一列的空格可以输出,不影响结果。 /*else space(strlen(words[i + row * j]) + 2, maxlen);*/ } printf("n"); } } return 0; } void space(int len, int maxlen) { int n = maxlen + 2 - len; for(int i = 0; i < n; i++) printf(" "); }


最后

以上就是寒冷唇膏最近收集整理的关于UVa 400 - Unix ls解题报告的全部内容,更多相关UVa内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部