本文主要是介绍400 - Unix ls,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:400 - Unix ls
题目大意:将给的字符串排序,然后要求最多的列,最少的行的格式输出, 并且要对齐, 对齐的长度是字符串最长的那个长度+2。
解题思路:用sort(),然后控制一下输出的顺序,一行一行的输出。注意:当心程序里有除以0的程序;
#include<stdio.h>
#include<stdlib.h>
#include<string.h>const int N = 105;
const int M = 65;
int t, len, cloumn, row;
char s[N][M];
int l[N];void findmax() {len = l[0] = strlen(s[0]);for(int i = 1; i < t; i++) {l[i] = strlen(s[i]);if(len < l[i]) len = l[i];}
}int cmp(const void * _a, const void * _b) {char * a = (char*) _a;char * b = (char*) _b;return strcmp(a,b);}int main() {int i, j, k;while(scanf("%d", &t) != EOF) {for(i = 0; i < t; i++)scanf("%s", s[i]);qsort(s, t, sizeof(s[0]), cmp);for(i = 0; i < 60; i++)printf("-");printf("\n");findmax();cloumn = 60 / (len + 2) == 0 ? 1 : 60 / (len + 2);if(t % cloumn == 0)row = t / cloumn;elserow = t / cloumn + 1;for(j = 0; j < row; j++) {for(i = j; i < t; i += row) {printf("%s",s[i]);for(k = 0; k < len + 2 - l[i]; k++)printf(" ");}printf("\n");}}return 0;
}
这篇关于400 - Unix ls的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!