版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_39651984/article/details/79032757
输入数据有若干列,不仅仅题目中的5列
1.找出每一列的最大长度
2.每行输出完最后的数据不能有空格,如 s: string;后面不能有空格。
3.按照格式输出
#include <bits/stdc++.h>
using namespace std;
typedef stringstream strstm;
#define MST(a,b) memset(a,b,sizeof(a));
#define _for(i,a,b) for(int i = (a); i < (b); ++i)
#define _rep(i,a,b) for(int i = (a); i <= (b); ++i)
const int maxn = 1000 + 1;
int len[maxn];
string s[maxn][180 + 1];
int main(int argc, char const *argv[])
{
string str,t; int row = 0;
int maxcol = 0;
while(getline(cin,str)){
int len = str.length();
strstm stream(str);
int col = 0;
while(stream >> t){
maxcol = max(maxcol, col);
s[row][col++] = t;
}
row++;
}
_for(i,0,180+1){
_for(j,0,maxn){
len[i] = max(len[i], (int)s[j][i].length());
}
}
_for(i,0,row){
cout.flags(ios::left);
_for(j,0,maxcol+1){
if(s[i][j] != "" && s[i][j+1] == "") cout << s[i][j];
else{
if(s[i][j] != "" && j < maxcol ) cout << setw(len[j]+1) << s[i][j];
if(s[i][j] != "" && j == maxcol) cout << s[i][j];
}
}
cout << endl;
}
return 0;
}