Algorithm 版 (精华区)
发信人: pamws (书虫), 信区: Algorithm
标 题: 1324K-Unix ls-ZJU
发信站: 哈工大紫丁香 (2002年10月11日22:13:51 星期五), 站内信件
// Vol.IV, 400 Unix ls of acm.uva.es
// Vol.III, 1324 Unix ls of acm.zju.edu.cn
#include <stdio.h>
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
#ifndef ONLINE_JUDGE
//close(0); open("400.in", O_RDONLY);
//close(1); open("400.out", O_WRONLY|O_CREAT, 0600);
#endif
int num=0;
while (scanf("%d", &num)!=EOF) {
if (num==0) break;
char **ls=new char *[num];
int idx=0;
int maxlen=0;
char buff[61];
for (int n=0; n<num; n++) {
scanf("%s", buff);
if (strlen(buff)>maxlen) maxlen=strlen(buff);
ls[idx]=new char[strlen(buff)+1];
strcpy(ls[idx], buff);
idx+=1;
}
for (int i=0; i<num-1; i++) {
int max=i;
for (int j=i; j<num; j++) {
if (strcmp(ls[max], ls[j])>0) max=j;
}
if (max!=i) {
char * tmp;
tmp=ls[max];
ls[max]=ls[i];
ls[i]=tmp;
}
}
printf("------------------------------");
printf("------------------------------");
printf("\n");
int col=(60-maxlen)/(maxlen+2)+1;
int row=ceil((float)num/(float)col);
for (int r=0; r<row; r++) {
for (int i=0; i<60; i++) buff[i]=' '; buff[60]='\0';
for (int c=0; c<col; c++) {
int base=c*(maxlen+2);
if ((c*row+r)<num) {
for (int j=0; j<strlen(ls[c*row+r]); j++) {
buff[base+j]=ls[c*row+r][j];
}
} else {
buff[base]='\0';
}
}
for (int i=59; i>=0; i--) {
if (isalnum(buff[i]) || buff[i]=='.' ||
buff[i]=='_' ||
buff[i]=='-') {
buff[i+1]='\0';
break;
}
}
printf("%s\n", buff);
}
}
return 0;
}
--
I Love Google...
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.118.226.228]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:4.315毫秒