UVa 1593 (水题 STL) Alignment of Code
话说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的更多相关文章
- UVa 10391 (水题 STL) Compound Words
今天下午略感无聊啊,切点水题打发打发时间,=_=|| 把所有字符串插入到一个set中去,然后对于每个字符串S,枚举所有可能的拆分组合S = A + B,看看A和B是否都在set中,是的话说明S就是一个 ...
- UVa 1595 (水题) Symmetry
颓废的一个下午,一直在切水题,(ˉ▽ ̄-) 首先如果这些点是对称的话,那么它们的对称轴就是x = m,m是横坐标的平均值. 把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这 ...
- UVa 10935 (水题) Throwing cards away I
直接用STL里的queue模拟即可. #include <cstdio> #include <queue> using namespace std; ; int discard ...
- UVa 400 (水题) Unix ls
题意: 有n个文件名,排序后按列优先左对齐输出.设最长的文件名的长度为M,则最后一列长度为M,其他列长度为M+2. 分析: 这道题很简单,但要把代码写的精炼,还是要好好考虑一下的.lrj的代码中有两个 ...
- UVa 11040 (水题) Add bricks in the wall
题意: 45块石头如图排列,每块石头上的数等于下面支撑它的两数之和,求其余未表示的数. 分析: 首先来计算最下面一行的数,A71 = A81 + A82 = A91 + 2A92 + A93,变形得到 ...
- UPC OJ 一道水题 STL
Problem C: 字符串游戏 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 10 Solved: 3 [Submit][Status][Web ...
- Square Numbers UVA - 11461(水题)
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> ...
- CODE FESTIVAL 2017 qual B B - Problem Set【水题,stl map】
CODE FESTIVAL 2017 qual B B - Problem Set 确实水题,但当时没想到map,用sort后逐个比较解决的,感觉麻烦些,虽然效率高很多.map确实好写点. 用map: ...
- UVa 489 HangmanJudge --- 水题
UVa 489 题目大意:计算机给定一个单词让你猜,你猜一个字母,若单词中存在你猜测的字母,则会显示出来,否则算出错, 你最多只能出错7次(第6次错还能继续猜,第7次错就算你失败),另注意猜一个已经猜 ...
随机推荐
- [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY
[初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY 发布者:sqqdugdu 时间:10-06 阅读数:2117 测试环境:RHEL 6.1,SecureCRT 5 ...
- MySQL数据库数据类型之集合类型SET测试总结
MySQL数据库提供针对字符串存储的一种特殊数据类型:集合类型SET,这种数据类型可以给予我们更多提高性能.降低存储容量和降低程序代码理解的技巧,前面介绍了首先介绍了四种数据类型的特性总结,其后又分别 ...
- ASP.NET 4.5新特性WebAPI从入门到精通
在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...
- POJ 1488
#include <iostream> #include <string> using namespace std; int main() { string s; int i; ...
- db2日期和时间常用汇总
1.db2可以通过SYSIBM.SYSDUMMY1.SYSIBM.DUAL获取寄存器中的值,也可以通过VALUES关键字获取寄存器中的值. SELECT 'HELLO DB2' FROM SYSIBM ...
- CentOS 6.5下安装Zabbix 2.2.x
操作系统:CentOS Mini 6.5 yum install httpd.x86_64 httpd-manual.x86_64 php-xml php-mbstring mysql-server ...
- select into from和insert into select from两种表复制语句区别
select into from和insert into select from两种表复制语句都是将源表source_table的记录插入到目标表target_table,但两句又有区别. 第一句(s ...
- lintcode:整数排序||
题目 给一组整数,按照升序排序.使用归并排序,快速排序,堆排序或者任何其他 O(n log n) 的排序算法. 解题 归并排序 public class Solution { /** * @param ...
- lintcode:单词切分
单词切分 给出一个字符串s和一个词典,判断字符串s是否可以被空格切分成一个或多个出现在字典中的单词. 样例 s = "lintcode" dict = ["lint&qu ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-007UserTypes的用法(@org.hibernate.annotations.Type、@org.hibernate.annotations.TypeDefs、CompositeUserType、DynamicParameterizedType、、、)
一.结构 二.Hibernate支持的UserTypes接口 UserType —You can transform values by interacting with the plain JD ...