您的位置:首页 > 其它

【Sorting】UVa400 Unix ls

2012-12-07 11:43 330 查看
好久没写解题报告了,本来准备刷完这些水题再开始写,不过面对昨晚做的这道题(UVa 400 / ZOJ 1324),简直让我吐血啊。先上图。





前后历经25次提交,终于通过。。。

虽然,他够水的。。

Unix ls

The computer company you work for is introducing a brand new computer line and is developing a new Unix-like operating system to be introduced along with the new computer. Your assignment is to write the formatter for the ls function.

Your program will eventually read input from a pipe (although for now your program will read from the input file). Input to your program will consist of a list of (F) filenames that you will sort (ascending based on the ASCII character values) and format into (C) columns based on the length (L) of the longest filename. Filenames will be between 1 and 60 (inclusive) characters in length and will be formatted into left-justified columns. The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters. Your program should use as few rows (R) as possible with rows being filled to capacity from left to right.

Input

The input file will contain an indefinite number of lists of filenames. Each list will begin with a line containing a single integer (1 ≤ N ≤ 100). There will then be N lines each containing one left-justified filename and the entire line's contents (between 1 and 60 characters) are considered to be part of the filename. Allowable characters are alphanumeric (a to z, A to Z, and 0 to 9) and from the following set { ._- } (not including the curly braces). There will be no illegal characters in any of the filenames and no line will be completely empty.

Immediately following the last filename will be the N for the next set or the end of file. You should read and format all sets in the input file.

Output

For each set of filenames you should print a line of exactly 60 dashes (-) followed by the formatted columns of filenames. The sorted filenames 1 to R will be listed down column 1; filenames R+1 to 2R listed down column 2; etc.

Sample Input

10

tiny

2short4me

very_long_file_name

shorter

size-1

size2

size3

much_longer_name

12345678.123

mid_size_name

12

Weaser

Alfalfa

Stimey

Buckwheat

Porky

Joe

Darla

Cotton

Butch

Froggy

Mrs_Crabapple

P.D.

19

Mr._French

Jody

Buffy

Sissy

Keith

Danny

Lori

Chris

Shirley

Marsha

Jan

Cindy

Carol

Mike

Greg

Peter

Bobby

Alice

Ruben

Sample Output

------------------------------------------------------------

12345678.123 size-1

2short4me size2

mid_size_name size3

much_longer_name tiny

shorter very_long_file_name

------------------------------------------------------------

Alfalfa Cotton Joe Porky

Buckwheat Darla Mrs_Crabapple Stimey

Butch Froggy P.D. Weaser

------------------------------------------------------------

Alice Chris Jan Marsha Ruben

Bobby Cindy Jody Mike Shirley

Buffy Danny Keith Mr._French Sissy

Carol Greg Lori Peter

题意是说给出N个字符串,排序以后按照列输出,每一行最多有60个字符,每个字符串输出的时候占据的宽度为所有N个字符串中最长的那个的长度加上2. 列数尽可能的多。

从这25次修改中,发现需要注意的有:

最后一列可能未排满,此时倒数第二列的单词后面不能再输出空格,否则会PE;

在刚开始应该将所有的数组初始化,这样在后来输出的时候不用考虑是否有空的;

单词长度范围为1≤len≤60,所以加上2以后很可能会超过60。如果按照我写的程序中计算r=60/max;此时max>60,则会出现除0的情况,即Runtime Error。

程序如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <algorithm>
const int N = 1000+2;
const int M = 600+2;
using namespace std;
struct data
{
char ch[M];
int len;
}num
;

bool cmp(data a, data b)
{
return strcmp(a.ch,b.ch) < 0 ;
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
int n;
while(scanf("%d",&n)!=EOF)
{
memset(num,0,sizeof(num));
printf("------------------------------------------------------------\n");
int max=0;
int r=0,c=0;
for(int i=1;i<=n;i++)
{
scanf("%s",num[i].ch);
num[i].len=strlen(num[i].ch);
if(max<num[i].len)
max=num[i].len;
}
sort(num,num+n+1,cmp);
max=max+2;
if(max>60)
max=60;
c=60/max;
r=n/c+(n%c?1:0);
if(c>n)
{
c=n;
r=1;
}
for(int i=1;i<=r;i++)
{
for(int j=1;j<=c;j++)
{
int d=r*(j-1)+i;
printf("%s",num[d].ch);
if(j!=c && num[d+r].ch[0]!='\0')
for(int k=0;k<max-num[d].len;k++)
printf(" ");
}
printf("\n");
}
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: