先计算出最长文件的长度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;
}

Uva - 400 - Unix ls的更多相关文章

  1. UVA 400 - Unix ls (Unixls命令)

    csdn : https://blog.csdn.net/su_cicada/article/details/86773007 例题5-8 Unixls命令(Unix ls,UVa400) 输入正整数 ...

  2. UVA 400 Unix ls by sixleaves

    题目其实很简单,答题意思就是从管道读取一组文件名,并且按照字典序排列,但是输入的时候按列先输出,再输出行.而且每一行最多60个字符.而每个文件名所占的宽度为最大文件名的长度加2,除了输出在最右边的文件 ...

  3. uva 400 Unix ls 文件输出排版 排序题

    这题的需要注意的地方就是计算行数与列数,以及输出的控制. 题目要求每一列都要有能够容纳最长文件名的空间,两列之间要留两个空格,每一行不能超过60. 简单计算下即可. 输出时我用循环输出空格来解决对齐的 ...

  4. UVa 400 Unix Is

    题意:给出n个字符串,按照字典序排列,再按照规则输出. ===学习的紫书,题目意思很清楚,求列数和行数最开始看的时候木有看懂啊啊啊 列数:即为(60-M)/(M+2)+1;即为先将最后那一列减去,算普 ...

  5. UVa - 1593 Unix ls(STL)

    给你一堆文件名,排序后按列优先的方式左对齐输出. 假设最长文件名长度是M,那么每一列都要有M+2字符,最后一列有M字符. inmanip真NB..orz #include <iostream&g ...

  6. UVa 400 Unix Is命令

    简单题 #include <bits/stdc++.h> using namespace std; const int maxn=110; string s[maxn]; int main ...

  7. UVa400.Unix ls

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 400 (13.08.05)

     Unix ls  The computer company you work for is introducing a brand new computer line and is developi ...

  9. Unix ls UVA - 400

      The computer company you work for is introducing a brand new computer line and is developing a new ...

随机推荐

  1. Linux学习之CentOS(四)----Linux文件属性、所有者、群组、其他组及文件权限操作简要总结

    Linux文件属性.所有者.群组.其他组及文件权限操作简要总结 首先介绍一个重要的知识点:文件属性控制权限 [root@www ~]# ls -al total 156 drwxr-x--- 4 ro ...

  2. 第二周个人作业WordCount

    1.Github地址 https://github.com/JingzheWu/WordCount 2.PSP表格 PSP2.1 PSP阶段 预估耗时 (分钟) 实际耗时 (分钟) Planning ...

  3. ionic 禁用 手势 滑动返回

    $ionicConfigProvider.views.swipeBackEnabled(false); 在 应用的 config里面 配置下 上面的那句话 就OK了.

  4. ajax中基本参数应用

    $(function () { $("#verificationCodeBtn").click(function () { $("#verificationCodeIma ...

  5. Node.js OS 模块

    Node.js os 模块提供了一些基本的系统操作函数.我们可以通过以下方式引入该模块: var os = require("os") 方法 序号 方法 & 描述 1 os ...

  6. PHP 5 String 函数

    PHP 5 String 函数 PHP String 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. 函数 描述 addcslashes() 返回在指定的字符前添加反斜杠的字符串. add ...

  7. Docker配置文件

    Docker 的 Registry 利用配置文件提供了一些仓库的模板(flavor),用户可以直接使用它们来进行开发或生产部署. 模板 在 config_sample.yml 文件中,可以看到一些现成 ...

  8. Azure AI 服务之语音识别

    笔者在前文<Azure AI 服务之文本翻译>中简单介绍了 Azure 认知服务中的文本翻译 API,通过这些简单的 REST API 调用就可以轻松地进行机器翻译.如果能在程序中简单的集 ...

  9. WiFi文件上传框架SGWiFiUpload

    背景 在iOS端由于文件系统的封闭性,文件的上传变得十分麻烦,一个比较好的解决方案是通过局域网WiFi来传输文件并存储到沙盒中. 简介 SGWiFiUpload是一个基于CocoaHTTPServer ...

  10. ORACLE异常(整理网上资料)

    一.oracle预定义异常 命名的系统异常 产生原因 Oracle Error SQLCODE Value ACCESS_INTO_NULL 未定义对象 ORA-06530  -6530 CASE_N ...