UVA 1593 Alignment of Code(紫书习题5-1 字符串流)
For the purpose of this problem, the code consists of multiple lines. Each line consists of one or more words separated by spaces. Each word can contain uppercase and lowercase Latin letters, all ASCII punctuation marks, separators, and other non-whitespace ASCII characters (ASCII codes 33 to 126 inclusive). Whitespace consists of space characters (ASCII code 32).
Input
Output
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp
Sample Output
start: integer; // begins here
stop: integer; // ends here
s: string;
c: char; // temp 关于代码对齐的练习 只需要找出每一列最长单词的长度,利用stringstream就可以轻松实现
vjudge 上的题目链接 :https://cn.vjudge.net/problem/POJ-3959;
关键字: vector , stringstream , setw() , string
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <iomanip>
using namespace std;
const int N = + ;
int max_len[]; //定义为全局列表 数组初始化为0
//记录每一列最长的长度
int main()
{
ios_base::sync_with_stdio(false);
string line;
int line_num = ;
vector<string> tail[N];
while(getline(cin,line)) //开始录入每一行的数据 遇到ctrl+z停止
{
int i=;
string word;
stringstream wordline(line); //word<<line 使用字符串流读取每一行的每一个单词
while(wordline>>word)
{
max_len[i] = max(max_len[i],int(word.length()));
tail[line_num].push_back(word);
++i;
}
++line_num;
}
for(int i = ;i < line_num;i++)
{
for(int j = ;j < tail[i].size(); j++)
{
cout<<setiosflags(ios::left)<<setw(max_len[j]+)<<tail[i][j];
//setiosflags(ios::left) 即控制向左输入
}
cout<<endl;
}
return ;
}
tips: stringstream 很方便,,但也很慢。
UVA 1593 Alignment of Code(紫书习题5-1 字符串流)的更多相关文章
- Uva - 1593 - Alignment of Code
直接用<iomanip>的格式输出,setw设置输出宽度,setiosflags(ios::left)进行左对齐. AC代码: #include <iostream> #inc ...
- UVA 1593: Alignment of Code(模拟 Grade D)
题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...
- UVA 1594 Ducci Sequence(紫书习题5-2 简单模拟题)
A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, · · · ...
- 紫书 习题 11-9 UVa 12549 (二分图最小点覆盖)
用到了二分图的一些性质, 最大匹配数=最小点覆盖 貌似在白书上有讲 还不是很懂, 自己看着别人的博客用网络流写了一遍 反正以后学白书应该会系统学二分图的,紫书上没讲深. 目前就这样吧. #includ ...
- 紫书 习题 11-8 UVa 1663 (最大流求二分图最大基数匹配)
很奇怪, 看到网上用的都是匈牙利算法求最大基数匹配 紫书上压根没讲这个算法, 而是用最大流求的. 难道是因为第一个人用匈牙利算法然后其他所有的博客都是看这个博客的吗? 很有可能-- 回归正题. 题目中 ...
- 紫书 习题8-12 UVa 1153(贪心)
本来以为这道题是考不相交区间, 结果还专门复习了一遍前面写的, 然后发现这道题的区间是不是 固定的, 是在一个范围内"滑动的", 只要右端点不超过截止时间就ok. 然后我就先考虑有 ...
- 紫书 习题8-7 UVa 11925(构造法, 不需逆向)
这道题的意思紫书上是错误的-- 难怪一开始我非常奇怪为什么第二个样例输出的是2, 按照紫书上的意思应该是22 然后就不管了,先写, 然后就WA了. 然后看了https://blog.csdn.net/ ...
- UVA 816 Abbott's Revenge 紫书
紫书的这道题, 作者说是很重要. 但看着题解好长, 加上那段时间有别的事, 磨了几天没有动手. 最后,这道题我打了五遍以上 ,有两次被BUG卡了,找了很久才找到. 思路紫书上有,就缺少输入和边界判断两 ...
- UVa 11582 Colossal Fibonacci Numbers! 紫书
思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161 的代码: #include <cstdio> # ...
随机推荐
- tp5.0:替换修改js、css等样式文件路径
首先, 我们要知道,TP5已经不支持绝对路径访问样式文件啦!所以我们不必去花时间去找使用$_SERVER来获取 手册位置:模板->内置标签->资源文件加载 方法一: 过程: 1.首先在模块 ...
- nginx-rtmp加入权限验证的简单方法
nginx-rtmp-module默认不限制推流权限.播放权限.如果想加入权限验证,有很多种方法. 方法一:修改源码如: 如何给 nginx rtmp 服务加入鉴权机制 http://blog.csd ...
- python入门7 字符串操作
字符串操作 #coding:utf-8 #/usr/bin/python """ 2018-11-03 dinghanhua 字符串操作 ""&quo ...
- note01-计算机网络
1.基础概述 三网: 电信网络.有线电视网络.计算机网络(连通&共享) 终端->z本地ISP->x地区IXP->y主干ISP 通信方式: C/S .P2P(即是client也 ...
- 模线性方程&&中国剩余定理及拓展
一.求解模线性方程 由ax=b(mod n) 可知ax = ny + b 就相当于ax + ny = b 由扩展欧几里得算法可知有解条件为gcd(a, n)整除d 可以直接套用扩展欧几里得算法 最终由 ...
- lua 语句学习
就如同C里的if else,while,do,repeat.就看lua里怎么用: 1.首先看if else t = {1,2,3} local i = 1 if t[i] and t[i] % 2 = ...
- STL - priority_queue(优先队列)
参考:http://www.cnblogs.com/xzxl/p/7266404.html 一.基本定义: 优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素.但是它有一个特性,就是队列中最大 ...
- 【转】Uint8Array 转为 string,解决中文乱码
来源: <http://stackoverflow.com/questions/8936984/uint8array-to-string-in-javascript/22373197> / ...
- Java中基本类型和引用类型(简单介绍)
8种基本类型 一.4种整型 byte 1字节 -128——127 short 2 字节 -32,768 —— 32,767 in ...
- js动画之requestAnimationFrame
1.setTimeout和setInterval 在讲setTimeout和setInterval之前,先讲一下异步执行的运行机制.(同步执行也是如此,因为它可以被视为没有异步任务的异步执行.) (1 ...