这道题目要求按照格式化输出文本,可能用stack数据结构会简单些,用max_h[2000]数组保存下每列最长单词长度就行,其他都很简单。(终于明白了ends原来与” “不一样呀,结果一直WA....)
#include <cstdio>
#include <iostream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <vector>
#define maxn 2000
using namespace std;
vector<string> s[maxn];
int main()
{
// freopen("input.txt","r",stdin);
int i=0,j;
int max_h[maxn];
string words;
memset(max_h,0,sizeof(max_h));
while(getline(cin,words)){
stringstream ss(words);
j=0;
while(ss>>words)
{
max_h[j]=max(max_h[j],(int)words.size());
j++;
s[i].push_back(words);
}
i++;
}
for(int w=0;w<i;w++)
{
for(j=0;j<s[w].size();j++)
{
cout<<s[w][j];
if(j==s[w].size()-1) cout<<endl;
else{
for(int p=1;p<=max_h[j]+1-(int)s[w][j].size();p++)
cout<<" ";
}
}
}
return 0;
}