Uva - 400 - Unix ls
2015-06-12 17:15
441 查看
先计算出最长文件的长度M,然后计算列数和行数,最后输出即可。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> using namespace std; const int maxcol = 60; const int maxn = 105; string filenames[maxn]; // 输出函数,s长度不足len时补字符extra void print(const string s, int len, char extra) { cout << s; for (int i = 0; i < len - s.length(); i++) { cout << extra; } } int main() { ios::sync_with_stdio(false); int n; while (cin >> n) { int M = 0; // 输出的列数 for (int i = 0; i < n; i++) { cin >> filenames[i]; M = max(M, (int)filenames[i].length()); } //计算列数cols和行数rows int cols = (maxcol - M) / (M + 2) + 1; int rows = (n - 1) / cols + 1; print("", 60, '-'); cout << endl; sort(filenames, filenames + n); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { int idx = c * rows + r; if (idx < n) { print(filenames[idx], c == cols - 1 ? M : M + 2, ' '); } } cout << endl; } } return 0; }
相关文章推荐
- 构建乘积数组
- Git-删除本地目录的repository(本地仓库)
- inux touch 命令 - 为什么我们需要改变时间戳?
- 移动端尺寸基础知识
- 【uiautomator】运行命令
- 多线程
- 菜鸟学习版本管理系统 Git 和 代码托管平台 GitHub
- os.rename 和 renames方法的使用
- 团队博客地址
- Linux 下使用Sigar 获取CPU 使用率
- SSL证书
- C Primer Plus (第五版) 第十六章 C预处理器和C库 编程练习
- unity 奇葩错误报错 nternal compiler error. See the console log for... 然后所有的代码都报 The associated script can
- 用LR做接口测试
- 低碳环保的有机肥造粒机
- Java Base64、HMAC、SHA1、MD5、AES DES 3DES加密算法
- TCP连接
- Connection closed by foreign host.
- 悬浮地板施工对返碱地面的处理
- vsftpd安装问题汇总(持续更新。。)