题目大意:将输入的文件名按 ascll 码顺序输出。输出从上到下,从左到右,每一行不能超过 60 个字符,除了最后一列外,每列的格式控制长度为最长的文件名的长度 +2。
解题思路:排序输出。输出格式好复杂- -…
#include<iostream>
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<cmath>
using namespace std;
char word[1000][100];
int cmp(const void*a,const void*b) {
return strcmp((char*)a,(char*)b);
}
int main() {
int N;
while(scanf("%d", &N) != EOF) {
int max = 0;
memset (word, 0, sizeof(word));
for (int i = 0; i < N; i++) {
scanf("%s", &word[i]);
if(strlen(word[i]) > max)
max = strlen(word[i]);
}
qsort(word, N, sizeof(word[0]), cmp);
for (int i = 0; i < 60; i++) printf("-");
printf("\n");
int c = 62 / (max + 2);
int r = N / c;
if(N % c) r++;
for (int i = 0; i < r; i++) {
int t = 0;
for (int j = i; j < N; j += r){
printf("%s", word[j]);
t++;
for (int k = strlen(word[j]) ; k < max; k++)
printf(" ");
if (t != c) printf(" ");
}
printf("\n");
}
}
return 0;
}