话说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 ;
}

代码君

最新文章

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

热门文章

  1. CSS预处理器实践之Sass、Less大比拼[转]
  2. centos mysql 操作
  3. iOS-CALayer遮罩效果
  4. UITbaleView上按钮的单选
  5. D&amp;F学数据结构系列——二叉排序树
  6. 【poj3691-DNA repair】AC自动机+DP
  7. 李洪强iOS开发之- 实现简单的弹窗
  8. libprotobuf ERROR
  9. C#程序大打开
  10. kill -9 和kill