话说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 = + ;
int width[maxn];
vector<string> a[maxn]; int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); string line, s;
int cnt = ;
while(getline(cin, line))
{
stringstream ss(line);
while(ss >> s) a[cnt].push_back(s);
cnt++;
} int col = ;
for(int i = ; i < cnt; i++) { int t = a[i].size(); col = max(col, t); }
for(int i = ; i < col; i++)
{
for(int j = ; j < cnt; j++) if(i < a[j].size())
{
int t = a[j][i].length() + ;
width[i] = max(width[i], t);
}
}
width[col - ]--;
cout.setf(ios::left); //左对齐
for(int i = ; i < cnt; i++)
{
for(int j = ; j < col && j < a[i].size(); j++)
{
if(j == a[i].size() - ) cout << std::setw(a[i][j].length());
else cout << std::setw(width[j]);
cout << a[i][j];
}
puts("");
} return ;
}

代码君

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

  1. UVa 10391 (水题 STL) Compound Words

    今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...

  2. UVa 1595 (水题) Symmetry

    颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...

  3. UVa 10935 (水题) Throwing cards away I

    直接用STL里的queue模拟即可. #include <cstdio> #include <queue> using namespace std; ; int discard ...

  4. UVa 400 (水题) Unix ls

    题意: 有n个文件名,排序后按列优先左对齐输出.设最长的文件名的长度为M,则最后一列长度为M,其他列长度为M+2. 分析: 这道题很简单,但要把代码写的精炼,还是要好好考虑一下的.lrj的代码中有两个 ...

  5. UVa 11040 (水题) Add bricks in the wall

    题意: 45块石头如图排列,每块石头上的数等于下面支撑它的两数之和,求其余未表示的数. 分析: 首先来计算最下面一行的数,A71 = A81 + A82 = A91 + 2A92 + A93,变形得到 ...

  6. UPC OJ 一道水题 STL

    Problem C: 字符串游戏 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 10  Solved: 3 [Submit][Status][Web ...

  7. Square Numbers UVA - 11461(水题)

    #include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...

  8. CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】

    CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...

  9. UVa 489 HangmanJudge --- 水题

    UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...

随机推荐

  1. 停止使用循环 教你用underscore优雅的写代码

    你一天(一周)内写了多少个循环了? var i; for(i = 0; i < someArray.length; i++) {   var someThing = someArray[i]; ...

  2. HDOJ 2152 Fruit(母函数)

    Fruit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  3. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...

  4. mysql之游标

    游标 行,也不存在每次一行地处理所有行的简单方法(相对于成批地处理它们).有时,需要在检索出来的行中前进或后退一行或多行.这就是使用游标的原因.游标( cursor)是一个存储在MySQL服务器上的数 ...

  5. Linux - wxWidgets安装和编译HelloWorld

    安装参考http://codelite.org/LiteEditor/WxWidgets30Binaries#toc2 源 /etc/apt/source.list deb http://repos. ...

  6. sshpass

    示例: ./sshpass -p ‘123456’  ssh -o StrictHostKeyChecking=no    root@192.168.1.15 ./sshpass -p ‘123456 ...

  7. [RM HA 1] Cloudera CDH5 RM HA功能验证

    简介: 最新的Cloudera CDH5.0.0 beta版本已经支持RM的HA, 笔者为此简单验证了RM HA的功能. 后续将继续分析其HA的原理,以及其与社区RM HA的区别. 集群部属与RM f ...

  8. 李洪强iOS开发之OC[008] -创建一个对象并访问实例变量

    // //  main.m //  07 - 创建一个对象并且访问实例变量 // //  Created by vic fan on 16/7/3. //  Copyright © 2016年 李洪强 ...

  9. lintcode:最长上升子序列

    题目 最长上升子序列 给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度. 样例 给出[5,4,1,2,3],这个LIS是[1,2,3],返回 3 给出[4,2,4,5,3,7],这个L ...

  10. iOS开发--成员变量与属性

    属性变量 @interface MyClass:NSObject{ MyObjecct *_object; } @property(nonamtic, retain) MyObjecct *objec ...