UVa 1593 (水题 STL) Alignment of Code

话说STL的I/O流用的还真不多,就着这道题熟练一下。

用了两个新函数:

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

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <sstream>
 4 #include <vector>
 5 #include <string>
 6 #include <cstring>
 7 #include <algorithm>
 8 #include <iomanip>
 9 using namespace std;
10
11 const int maxn = 1000 + 10;
12 int width[maxn];
13 vector<string> a[maxn];
14
15 int main()
16 {
17     //freopen("in.txt", "r", stdin);
18     //freopen("out.txt", "w", stdout);
19
20     string line, s;
21     int cnt = 0;
22     while(getline(cin, line))
23     {
24         stringstream ss(line);
25         while(ss >> s) a[cnt].push_back(s);
26         cnt++;
27     }
28
29     int col = 0;
30     for(int i = 0; i < cnt; i++) { int t = a[i].size(); col = max(col, t); }
31     for(int i = 0; i < col; i++)
32     {
33         for(int j = 0; j < cnt; j++) if(i < a[j].size())
34         {
35             int t = a[j][i].length() + 1;
36             width[i] = max(width[i], t);
37         }
38     }
39     width[col - 1]--;
40     cout.setf(ios::left);   //左对齐
41     for(int i = 0; i < cnt; i++)
42     {
43         for(int j = 0; j < col && j < a[i].size(); j++)
44         {
45             if(j == a[i].size() - 1) cout << std::setw(a[i][j].length());
46             else cout << std::setw(width[j]);
47             cout << a[i][j];
48         }
49         puts("");
50     }
51
52     return 0;
53 }

代码君

时间: 2024-08-04 17:39:24

UVa 1593 (水题 STL) Alignment of Code的相关文章

UVa 10391 (水题 STL) Compound Words

今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个复合词. 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <string> 6 #include <

UVa 1595 (水题) Symmetry

颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这个点是否在集合里面. 如果有一个不在的话,说明不能构成对称图形. 1 #include <cstdio> 2 #include <algorithm> 3 #include <set> 4 using namespace std; 5 6 struct Point 7 {

UVa 10935 (水题) Throwing cards away I

直接用STL里的queue模拟即可. 1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 const int maxn = 60; 6 int discarded[maxn], cnt; 7 8 int main() 9 { 10 int n; 11 while(scanf("%d", &n) == 1 && n) 12 { 13 cnt = 0; 14 qu

UVa 400 (水题) Unix ls

题意: 有n个文件名,排序后按列优先左对齐输出.设最长的文件名的长度为M,则最后一列长度为M,其他列长度为M+2. 分析: 这道题很简单,但要把代码写的精炼,还是要好好考虑一下的.lrj的代码中有两个亮点,一个是print子函数,一个就是行数的计算.用心体会 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <algorithm> 5 using namespac

UPC OJ 一道水题 STL

Problem C: 字符串游戏 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 10  Solved: 3 [Submit][Status][Web Board] Description 说到游戏,大家还是比较喜欢的,但是玩游戏,如果想赢得对方还是得靠思维的,xy比较喜欢字符串,尤其是起始字母等于终止字母这样的字符串(the string's length must exceed one),但是呢,xy有个癖好,喜欢把每个字符重新分配一个值,喜欢

UVa 11040 (水题) Add bricks in the wall

题意: 45块石头如图排列,每块石头上的数等于下面支撑它的两数之和,求其余未表示的数. 分析: 首先来计算最下面一行的数,A71 = A81 + A82 = A91 + 2A92 + A93,变形得到A92 = (A71 - A91 - A93) / 2. 以此类推,就能得到最下面一整行的数.有了这个“地基”以后,所有的数就都能算出来了. 1 #include <cstdio> 2 3 int a[10][10]; 4 5 int main() 6 { 7 //freopen("in

uva 1368 水题

枚举每一列的位置,求哪个字符出现的次数最多 #include<iostream> #include<string> #include<map> #include<cstdio> #include<vector> #include<algorithm> #include<assert.h> #include<cstring> using namespace std; #define _for(i, a, b) f

UVa 1593 Alignment of Code(字符串)

题意  按要求对齐代码 字符串流的应用 #include <bits/stdc++.h> using namespace std; const int N = 1005, M = 200; string s[N][M], line; int cw[M], cn[N]; int main() { int r = 0, c = 0; while(getline(cin, line)) { stringstream ss(line); while(ss >> s[r][c]) { if(

UVa 1586 Molar mass --- 水题

UVa 1586 题目大意:给出一种物质的分子式(不带括号),求分子量.本题中分子式只包含4种原子,分别为C.H.O.N, 原子量分别为12.01,1.008,16.00,14.01 解题思路:先实现一个从字符型的数到整型的数的转换函数,再将输入的串从头到尾扫描,遇到字母,则进一步扫描后面的数字的区间, 再调用函数进行转换,再乘以其的原子质量,最后累加到sum中即可. /* UVa 1586 Molar mass --- 水题 */ #include <cstdio> #include <