您的位置:首页 > 其它

UVa 1593 (水题 STL) Alignment of Code

2015-04-24 11:42 351 查看
话说STL的I/O流用的还真不多,就着这道题熟练一下。

用了两个新函数:

cout << std::setw(width[j]); 这个是设置输出宽度的,但是默认是在右侧补充空格

所以就要用cout.setf(ios::left);来设置一下左对齐。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <iomanip>
using namespace std;

const int maxn = 1000 + 10;
int width[maxn];
vector<string> a[maxn];

int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);

string line, s;
int cnt = 0;
while(getline(cin, line))
{
stringstream ss(line);
while(ss >> s) a[cnt].push_back(s);
cnt++;
}

int col = 0;
for(int i = 0; i < cnt; i++) { int t = a[i].size(); col = max(col, t); }
for(int i = 0; i < col; i++)
{
for(int j = 0; j < cnt; j++) if(i < a[j].size())
{
int t = a[j][i].length() + 1;
width[i] = max(width[i], t);
}
}
width[col - 1]--;
cout.setf(ios::left);   //左对齐
for(int i = 0; i < cnt; i++)
{
for(int j = 0; j < col && j < a[i].size(); j++)
{
if(j == a[i].size() - 1) cout << std::setw(a[i][j].length());
else cout << std::setw(width[j]);
cout << a[i][j];
}
puts("");
}

return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: