400 - Unix ls
来源:互联网 发布:微信小程序 源码下载 编辑:程序博客网 时间:2024/05/20 20:47
The computer company you work for isintroducing a brand new computer line and is developing a new Unix-likeoperating system to be introduced along with the new computer. Your assignmentis to write the formatter for the ls function.
Your program will eventually read input froma pipe (although for now your program will read from the input file). Input toyour 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 willbe between 1 and 60 (inclusive) characters in length and will be formatted intoleft-justified columns. The rightmost column will be the width of the longestfilename and all other columns will be the width of the longest filename plus2. There will be as many columns as will fit in 60 characters. Your programshould use as few rows (R) as possible with rows being filled tocapacity from left to right.
Input
The input file will contain an indefinitenumber of lists of filenames. Each list will begin with a line containing asingle integer ( ).There will then be N lines each containing one left-justifiedfilename and the entire line's contents (between 1 and 60 characters) areconsidered to be part of the filename. Allowable characters are alphanumeric (ato z, A to Z, and 0 to 9) and from thefollowing set { ._- } (not including the curly braces). There will beno illegal characters in any of the filenames and no line will be completelyempty.
Immediately following the last filename willbe the N for the next set or the end of file. You should readand format all sets in the input file.
Output
For each set of filenames you should print aline 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
代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<iomanip>
using namespacestd;
int main()
{
cout.setf(ios::left);
int sum;
while(cin>>sum)
{
string s[110];
int m=0;
for(int i=0;i<sum;i++)
{
cin>>s[i+1];
if(s[i+1].size()>m)
{
m=s[i+1].size();
}
}
int c=(60-m)/(m+2)+1;
int r=(sum-1)/c+1;
sort(s+1,s+sum+1);
cout<<"------------------------------------------------------------\n";
for(int i=1;i<=r;i++)
{
for(int j=1;j<c;j++)
{
if((i+(j-1)*r)<=sum)
{
cout<<setw(m+2)<<s[i+(j-1)*r];
}
}
if((i+(c-1)*r)<=sum)
{
cout<<setw(m)<<s[i+(c-1)*r];
}
cout<<endl;
}
}
}
题意:
题目:对输入的每组文件名进行规则化输出,除了最后一列,其他列的宽度为maxlen+2,最后一列宽度为maxlen。maxlen为最长字符串的长度。
对字符串排序,然后计算相应长度,得出maxlen。现在需要确定列数和行数。列数:设有n+1列。则(maxlen+2)*n+maxlen<=60,则n<=(60-maxlen)/(maxlen+2),n是整数,且n需要最大化,则n即为右边值。行数:对( q 除以 lie )取上整,这里的q是总共字符串数。这里是从左往右排,一列排满了再排下一列。注意整数相除+1,-1的调试
- UVa 400 - Unix ls
- uva 400 Unix ls
- UVA 400 - Unix ls
- 400 - Unix ls
- uva 400 - Unix ls
- uva-400-Unix ls
- 400 - Unix ls
- UVaOJ 400 - Unix ls
- UVa 400 - Unix ls
- 400 - Unix ls
- UVA 400 Unix ls
- UVa 400 - Unix ls
- uva 400 - Unix ls
- UVa - 400 - Unix ls
- uva 400 Unix ls
- 400 - Unix ls
- UVa 400 - Unix ls
- UVa 400 - Unix ls
- 136 - Ugly Numbers
- 156 - Ananagrams
- Linux菜鸟必学的60个命令
- 207 - PGA Tour Prize Money
- 221 - Urban Elevations
- 400 - Unix ls
- 540 - Team Queue
- 814 - The Letter Carrier's Rounds
- 1592 - Database
- 10474 - Where is the Marble
- 10815 - Andy's First Dictionary
- 12096 - The SetStack Computer
- 212 - Use of Hospital Facilities
- HDU 1575 Tr A (矩阵快速幂)