Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
题意:输入数行数据,一行数据多个短字符串,输出要按照每列最长的占位进行输出。
下面是具体显示效果(为了看清楚具体输出我把空格换成了"|")
输入:
--------------------------------------------
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp
输出:
--------------------------------------------
start:|integer;|//|begins|here
stop: |integer;|//|ends |here
s: |string;
c: |char; |//|temp
代码:
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<set>
#include<sstream>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
static int len[50];//记录每列字符串的最大长度
int main(void)
{
string s,one;
vector<string>list[1001];//每一个vector都是一个数组,类似于可变长的二维数组
vector<string>::iterator it;
int hang=0,lie,maxlen=0;
while (getline(cin,s))
{
lie=0;
istringstream sin(s);//重定向
while (sin>>one)
{
list[hang].push_back(one);//加入到自己所在行
len[lie]=max(len[lie],(int)one.size());//每列的最大长度
lie++;//列数更新
}
hang++;//行数更新
}
for (int i=0; i<hang; i++)
{
for (int j=0; j<list[i].size(); j++)
{
if(j!=list[i].size()-1)
cout<<left<<setw(len[j])<<list[i][j]<<" ";//输出占位+一个空格
else
cout<<left<<list[i][j]<<endl;//输出占位不加空格
}
}
return 0;
}