-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path400.c
51 lines (47 loc) · 1.11 KB
/
400.c
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
#include<stdio.h>
#include<string.h>
#define MAX 100 + 5
#define WIDTH 60
int char_cmp(char* x, char* y);
int main() {
int list_len;
char names[MAX][WIDTH];
int name_len_max;
int columns;
int rows;
while(scanf("%d", &list_len) != EOF) {
name_len_max = 0;
int i;
for(i = 0; i < list_len; i++) {
scanf("%s", names[i]);
int j = strlen(names[i]);
if(j > name_len_max)
name_len_max = j;
}
qsort(names, list_len, WIDTH * sizeof(char), char_cmp);
columns = (WIDTH + 2)/(name_len_max + 2);
if((list_len % columns) == 0)
rows = list_len / columns;
else
rows = list_len / columns + 1;
for(i = 0; i < 60; i++)
printf("-");
printf("\n");
int j;
for(i = 0; i < rows; i++) {
int is_last_blank = (list_len%rows != 0)&&(i >= list_len%rows);
for(j = 0; j < columns - is_last_blank; j++) {
int index = j*rows + i;
printf("%s", names[index]);
int k;
for(k = 0; k < name_len_max - strlen(names[index]) + 2; k++)
printf(" ");
}
printf("\n");
}
}
return 0;
}
int char_cmp(char* x, char* y) {
return strcmp(x, y);
}