踩坑
简单来说,因为(对内存)抠门,我堵了半个多小时……即他数据最多1000行,你给他个1003行不行!真的,就是因为这种原因。
(顺嘴,记得输出不要在每行末尾加空格哦~
AC代码
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
#include <algorithm>
#include <cstring>
//#define TEST//测试用
using namespace std;
const int max_n=1000+3;//就是你,浪费我时间啊!!
string words[max_n][180];
string line[max_n];
int cols[max_n][180];//一行中每个单词的长度
int max_col[180];//每列最大的单词长度
int max_words[max_n];//每行最大单词数
int main(){
//测试用,不懂自行百度
#ifdef TEST
freopen("D:\\college\\clionworkplace\\test_c++\\in.txt","r",stdin);
freopen("D:\\college\\clionworkplace\\test_c++\\out.txt","w",stdout);
#endif
int i=0;
memset(max_col,0,sizeof(max_col));
while (getline(cin,line[i])){
stringstream ss(line[i]);
int j=0;
while (ss>>words[i][j]){
cols[i][j]=words[i][j].length();
max_col[j]=max(max_col[j],cols[i][j]);//每一列最大的字符长度
++j;
}
max_words[i]=j;
++i;
}
for(int t1=0;t1<i;t1++){
for(int t2=0;t2<max_words[t1];t2++){
cout<<words[t1][t2];
if(t2+1==max_words[t1]){
break;
}
for(int t3=0;t3<max_col[t2]-cols[t1][t2]+1;t3++){
printf(" ");
}
}
printf("\n");
}
return 0;
}