UVa 400 -- Unix ls

 UVa 400 -- Unix ls

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

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxcol = 60;
 7 const int maxn = 100 + 5;
 8 string filenames[maxn];
 9 
10 void print(const string& s,int len,char extra)
11 {
12     cout<<s;
13     for(int i=s.length()+1;i<=len;i++)
14     {
15         cout<<extra;
16     }
17 }
18 
19 int main()
20 {
21     int n;
22     while(cin>>n)
23     {
24         int M = 0;
25         for(int i = 0;i<n;i++)
26         {
27             cin>>filenames[i];
28             M = max(M,(int)filenames[i].length());
29         }
30         ///计算行数和列数
31         int col = (maxcol-M)/(M+2)+1;
32         int row = (n-1)/col + 1;
33 
34         sort(filenames,filenames+n);//排序
35 
36         print("-",60,'-');
37         cout<<endl;
38         for(int i=0;i<row;i++)
39         {
40             for(int j=0;j<col;j++)
41             {
42                 if((j*row+i)>=n) break;
43                 if(j == col-1) print(filenames[j*row+i],M,' ');
44                 else print(filenames[j*row+i],M+2,' ');
45             }
46             cout<<endl;
47         }
48 
49 
50     }
51     return 0;
 
 
结果:
10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
------------------------------------------------------------
12345678.123         size-1
2short4me            size2
mid_size_name        size3
much_longer_name     tiny
shorter              very_long_file_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
------------------------------------------------------------
Alfalfa        Cotton         Joe            Porky
Buckwheat      Darla          Mrs_Crabapple  Stimey
Butch          Froggy         P.D.           Weaser
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben
------------------------------------------------------------
Alice       Chris       Jan         Marsha      Ruben
Bobby       Cindy       Jody        Mike        Shirley
Buffy       Danny       Keith       Mr._French  Sissy
Carol       Greg        Lori        Peter
 

转载于:https://www.cnblogs.com/yxh-amysear/p/8428263.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值