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