思路:麻烦在于输出的格式,lamx 为字符串中最长的,所以c = 62 / (lmax + 2), r = (n - c + 1) / c;
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char s[105][105];
int cmp_str(void const *a, void const *b){
return strcmp((char *)a, (char *)b);
}
int main(){
int n, lmax, c, r, m;
while (scanf("%d", &n) != EOF){
lmax = 0;
getchar();
for(int i = 0; i < n; i++){
gets(s[i]);
if (strlen(s[i]) > lmax)
lmax = strlen(s[i]);
}
qsort(s, n, sizeof(s[0]), cmp_str);
printf("------------------------------------------------------------\n");
c = 62 / (lmax + 2);
r = (n - 1 ) / c + 1;
for(int i = 0; i < r; i++){
for(int j = 0; j < c; j++){
if (j * r + i >= n)
break;
printf("%s", s[j * r + i]);
m = lmax - strlen(s[j * r + i]);
for(int k = 0; k < m; k++)
printf(" ");
if (j != c)
printf(" ");
}
printf("\n");
}
}
return 0;
}