UVa 1593 (水题 STL) Alignment of Code
2024-10-15 12:16:52
话说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 ;
}
代码君
最新文章
- 简述HTML DOM及其节点分类
- 常用COBOL函数
- jQuery Countdown Timer 倒计时效果
- 在多线程环境中使用CoreData
- unity3d AssetBundle包加密
- JAVA 界面 - 记事本
- 【HTML5 3】《html5 开发精要与实例讲解》 step2 -- 结构元素 (待续)
- ###使用phpmailer自动邮件提醒
- 这样就算会了PHP么?-10
- 通过一个模拟程序让你明白WCF大致的执行流程
- [Ext JS 4] 布局之实战一 - 中间区块不会自动伸展 (tab)
- 自动化运维:使用psutil和paramiko读取远程主机信息
- git使用命令总结
- 最新版redis的安装及配置 linux系统
- Jenkins+Gradle实现android开发持续集成、打包
- springmvc结合ajax的分页功能
- Mybatis使用动态代理实现拦截器功能
- 【转载】JVM系列二:GC策略&;内存申请、对象衰老
- beginner_json_setting
- JAVA引用的种类