博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Sorting】UVa400 Unix ls
阅读量:4568 次
发布时间:2019-06-08

本文共 4135 字,大约阅读时间需要 13 分钟。

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

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

虽然,他够水的。。

  

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 
#include
#include
#include
#include
const int N = 1000+2;const int M = 600+2;using namespace std;struct data{ char ch[M]; int len;}num[N];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
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

  

转载于:https://www.cnblogs.com/wuxinlei/archive/2012/12/07/2806909.html

你可能感兴趣的文章
java学习第八天
查看>>
判断是否有人在操作某张表,并获取…
查看>>
第四周仿真与计算作业
查看>>
.net中WebService的使用实例
查看>>
editplus的配色
查看>>
最近3年股息率最高排名
查看>>
ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph
查看>>
IO流(四)
查看>>
Java 序列化Serializable
查看>>
Spring在web请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
查看>>
[数据库基础]——编码标准之结构
查看>>
c++模版函数
查看>>
android Fragments详解二:创建Fragment
查看>>
VMware Tools (ubuntu系统)安装详细过程与使用
查看>>
Oracle性能优化-读懂执行计划
查看>>
关于VC弹出选择文件夹对话框
查看>>
perl多线程理解
查看>>
[原]three.js绘制过程(二)
查看>>
[SHOI2009] 交通网络
查看>>
supervisor 监控redis & mongodb
查看>>