代码对齐--string|stream

时间:2022-04-06 20:01:44

https://vjudge.net/contest/185301#problem/A

学习了好多新函数,只要关注每个单词的列宽就行了。自动切分太厉害了吧~

 #include<iomanip>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<string>
#include<set>
#include<cctype>
#include<sstream>
#define mem(a) memset(a,0,sizeof(a))
#define LL long long
#define ull unsigned long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=2e4+;
int n,m;
int a[N],b[N],c[N][N];
vector<string>s[]; int main()
{
string line,tem;
int v=,c[],u=;
mem(c);
while(getline(cin,line))
{
istringstream ans(line); //自动切分|空格|读入
while(ans>>tem)
{
c[v]=max(c[v],(int)tem.size());
s[u].push_back(tem);
v++;
}
v=,u++;
}
cout<<setiosflags(ios::left);//左对齐
for(int i=;i<u;i++)
{
int j;
for(j=;j<s[i].size()-;j++)
{cout <<setw(c[j]+)<<s[i][j];} // 设置s[i][j]域宽 cout<<s[i][j]<<endl;
} return ;
}