Uva 400.Unix is

怎么看这道题都是理解比实现难

特别是英语版,最后如何输出被绕进去了

大概意思就是在保证每行不超过60字符的情况下,尽可能让行数最少输出。

注意三目运算符的运算优先度,记得加括号

 1 #include <cstdio>
 2 #include <string>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <vector>
 6 #include <algorithm>
 7 using namespace std;
 8 
 9 class LOVE{
10     private:
11         static const int len=65;
12         static const int maxn=105;
13 
14     public:
15         bool Do(){
16             int n,Max=0;
17             if(!(cin>>n))
18                 return false;
19             cout<<"------------------------------------------------------------
";
20             string str[maxn];
21             for(int i=0;i<n;i++){
22                 cin>>str[i];
23                 Max=max(Max,(int)str[i].size());
24             }
25             sort(str,str+n);
26 
27             int L,H;
28             for(int i=n;i>0;i--){
29                 L=i;
30                 H=n/L+(n%L?1:0);
31                 int size=Max*L+2*(L-1);
32                 if(size<=60)break;
33             }
34             //cout<<"
"<<L<<" "<<H<<"
";
35             for(int i=0;i<H;i++){
36                 for(int j=0;j<L;j++){
37                     cout<<str[i+j*H];
38                     for(int k=0;k<(j==L-1?0:2)+Max-(int)str[i+j*H].size();k++)
39                         cout<<" ";
40                 }
41                 cout<<"
";
42             }
43             return true;
44         }
45 };
46 
47 int main(){
48     //freopen("in.txt","r",stdin);
49     std::ios::sync_with_stdio(false);
50     LOVE LIVE;
51     while(LIVE.Do());
52     return 0;
53 }
原文地址:https://www.cnblogs.com/ohyee/p/5222207.html