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> # ...
随机推荐
- SpringMvc-helloword
说明:在此只说明helloword的简单实现,通过helloword例子先了解springMvc是这样工作的,然后在一步步的研究原理 配置web.xml 1.配置servlet servlet-cla ...
- pip 安装下载好的tensorflow
pip --default-timeout=100 install C:\Users\Administrator\Downloads\tensorflow-1.12.0-cp37-cp37m-win_ ...
- Unix 和 Linux 安装 Perl
Unix/Linux 系统上 Perl 安装步骤如下: 通过浏览器打开 http://www.perl.org/get.html. 下载适用于 Unix/Linux 的源码包. 下载 perl-5.x ...
- Jmeter入门19 保存测试结果(或从文件读取结果)
以聚合报告为例,其他监听器有write results to file的类似. 首先 为了避免每次保存的测试报告被覆盖,我们在testplan下添加两个参数:项目名和当前时间(毫秒级) 其次 添加聚合 ...
- Jmeter入门16 数据构造之随机数Random Variable & __Random函数
接口测试有时参数使用随机数构造.jmeter添加随机数两种方式 1 添加配置 > Random Variable 2 __Random函数 ${__Random(1000,9999) ...
- C语言 字符串处理函数
#include <stdio.h> #include <string.h> // strlen void test() { // 测量字符串常量的字符长度(不包括\0这个字符 ...
- Codeforces 396A 数论,组合数学
题意:给一个a数组,求b 数组的方案数,但是要求两者乘积相同. 分析: 不可能将它们乘起来,对于每个数质因数分解,得到每个质因子个数,遍历这些质因子,将某个质因子放到 对应的盒子里面,可以不放,方案数 ...
- 【[USACO16OPEN]262144】
发现这个数列的范围特别大但是值域的范围特别小 于是可以大胆猜测这道题值域肯定需要开到状态里去 又发现\(262144=2^{18}\)这个暗示非常明显啊,暗示这道题跟二进制有关系 其实也没什么关系 设 ...
- [19/03/20-星期三] 常用类_Enum(枚举)类
一.概念(JDK 1.5之后才有的类) 所有的枚举(英语:enumeration) 类型隐性地继承自 java.lang.Enum.枚举实质上还是类,而每个被枚举的成员实质就是一个枚举类型的实例,他们 ...
- 2018.12.19 Struts2 框架总复习
总结Struts2 框架 struts2技术的优势 项目开源,使用及拓展方便 提供Exception处理机制 Result方式的页面导航,通过Result标签很方便的实现重定向和页面跳转 通过简单.集 ...