UVa 1593 代码对齐

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main(){
 6     vector<string> ans[1001];
 7     int length[1000] = {0};
 8     string now;
 9     int i = 0;
10     while(cin >> now){
11         ans[i].push_back(now);
12         int t = (int)ans[i].size(),p = (int)now.length();
13         if(p > length[t - 1])length[t-1] = p;
14         if(cin.peek() == '\n')i++;
15     }
16     int m = 1;
17     for(int j = 0; j < i; j++){
18         if(m)m = 0;else cout<<endl;
19         int first = 1;
20         int k = (int)ans[j].size();
21         for(int f = 0; f < k; f++){
22             if(first)first = 0;else cout<<" ";
23             int l = (int)ans[j][f].length();
24             cout<<ans[j][f];
25             int g = length[f];
26             for(int r = 0; r < g - l; r++)cout<<" ";
27         }
28     }
29 }

主要注意: 1. 用cin自动排除空格  2. 统计每一行的对应列的单词长度最值,方便以后补空格输出。

上一篇:UVA - 11987 Almost Union-Find


下一篇:uva 1335 - Beijing Guards(二分)