输入若干行代码,要求各列单词的左边界对齐且尽量靠左。单词之间至少要空一格。每个单词不超过80个字符,每行不超过180个字符,一共最多1000行,样例输入与输出如图所示。
//代码对齐
//思路:统计每列最长的单词 不够长度补空格
#include<cstdio>
#include<iostream>
#include<vector>
#include<sstream>
using namespace std;
vector<string> words[1005]; //相当于二维数组
int maxn[180]; // 保存每列最长的长度
int main() {
string line;
int row = 0, col = 0;
while(getline(cin, line))
{ // 输入
string buf;
stringstream ss(line);
while(ss>>buf)
{
words[row].push_back(buf);
int len = buf.size();
maxn[col] = max(maxn[col], len); // 记录一列中最长长度
col ++;
}
row++;
}
for(int i = 0; i < row; i++)
{
for(int j = 0; j < words[i].size(); j++)
{
if(j) cout << " ";
cout << words[i][j];
if(j == words[i].size() - 1) {cout << endl; break;} // 最后一列的数不用补空格,直接输出
for(int k = words[i][j].length(); k < maxn[j]; k++) cout << " ";
}
}
return 0;
}