UVa 1593 (水题 STL) Alignment of Code
2015-04-24 11:42
351 查看
话说STL的I/O流用的还真不多,就着这道题熟练一下。
用了两个新函数:
cout << std::setw(width[j]); 这个是设置输出宽度的,但是默认是在右侧补充空格
所以就要用cout.setf(ios::left);来设置一下左对齐。
代码君
用了两个新函数:
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; }
代码君
相关文章推荐
- Alignment of Code UVA - 1593(水题)
- UVA 1593 Alignment of Code
- UVA1593 - Alignment of Code(这题确实看不太懂)
- UVA - 1593 Alignment of Code(熟练c++操作)
- UVa 1593 - Alignment of Code
- UVA 1593(p135)----Alignment of Code
- UVa--1593 Alignment of Code(string)
- Uva1593 - Alignment of Code(getline、istringstream用法)
- UVa 1593 - Alignment of Code
- UVa 1593 Alignment of Code
- [刷题]算法竞赛入门经典(第2版) 5-1/UVa1593 - Alignment of Code
- Uva - 1593 - Alignment of Code
- UVa 1593 - Alignment of Code
- Uva - 1593 - Alignment of Code
- Problem 043——UVa 1593 - Alignment of Code
- 【习题5-1 UVA - 1593】Alignment of Code
- Uva1593——Alignment of Code
- UVA 1593 Alignment of Code
- UVa 1593 - Alignment of Code(格式+细节)
- UVA - 1593 Alignment of Code