UVA 1593 Alignment of Code

题意:

输入后对齐输出。

思路:

①用一个vector数组装每行的字符。

②用整型数组len存每列最大的长度加一(多算一个空格),比如在样例输入中,第一列最大的是start:。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<vector>
 5 #include<sstream>
 6 #include<cstdio>
 7 #include<set>
 8 #include<cstring>
 9 using namespace std;
10 vector<string> vtr[1005];
11 string str;
12 int len[185];//记录每列string最大的长度
13 int main()
14 {
15     int cnt = 0;//数行数
16     memset(len, 0, 185);
17     while (getline(cin, str))
18     {
19         stringstream ss(str);
20         string word;
21         int cnt_word = 0;//数单词数
22         while (ss >> word)
23         {
24             vtr[cnt].push_back(word);
25             //每次比较,存入最大的长度,length+1是因为最少都要一个空格
26             len[cnt_word] = len[cnt_word] > (word.length() + 1) ? len[cnt_word] : word.length() + 1;
27             cnt_word++;
28         }
29         cnt++;
30     }
31 
32     for (int i = 0; i < cnt; i++)//行
33     {
34         for (int j = 0; j < vtr[i].size(); j++)//列
35         {
36             if (len[j]> vtr[i][j].length())//不够长要补空格
37             {
38                 cout << vtr [i][j];
39                 for (int k = 0;j!= vtr[i].size()-1&& k < len[j] - vtr[i][j].length(); k++)
40                 {
41                     cout << " ";
42                 }
43             }
44             else
45             {
46                 cout << vtr[i][j];
47             }
48         }
49         
50         cout << "\n";
51     }
52 
53     return 0;
54 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值