原题链接:https://vjudge.net/problem/UVA-1593
分类:<string>
备注:水题
思路:每一列的最长长度记录下下来。
注意:最后一列的单词直接输出别补空格!
代码如下:
#include<vector>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxh = 1000 + 5;
const int maxw = 180 + 5;
int main(void)
{
vector<string>out[maxh];
string s, x;
int maxlen[maxw] = { 0 }, line = 0;
while (getline(cin, s))
{
stringstream ss(s);
int pos = 0;
while (ss >> x)
{
out[line].push_back(x);
maxlen[pos] = max(maxlen[pos], (int)x.length());
pos++;
}
line++;
}
for (int i = 0; i < line; i++)
{
for (int j = 0; j < out[i].size(); j++)
{
if (j)cout << " ";
cout << out[i][j];
if (j == out[i].size() - 1) { cout << "\n"; break; }
for (int k = out[i][j].length(); k < maxlen[j]; k++) cout << " ";
}
}
return 0;
}