先计算出最长文件的长度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. Gethub readme 撰写

    大标题=== 小标题----- #一级标题 ##二级标题 ###三级标题 ####四级标题 #####五级标题 ######六级标题 插入圆点* 昵称:果冻虾仁 * 别名:隔壁老王 * 英文名:Jel ...

  2. Servlet生命周期与工作原理(转载)

    Servlet生命周期分为三个阶段: 1,初始化阶段  调用init()方法 2,响应客户请求阶段 调用service()方法 3,终止阶段 调用destroy()方法 Servlet初始化阶段: 在 ...

  3. 用命令直接在两台ubuntu之间传输数据

    首先查看openssh-server是否启动: ps -e | grep ssh 如果没有任何提示则是没有启动: sudo /etc/init.d/ssh -start 启动进程.若提示找不到命令则需 ...

  4. Go 语言切片(Slice)

    Go 语言切片是对数组的抽象. Go 数组的长度不可改变,在特定场景中这样的集合就不太适用,Go中提供了一种灵活,功能强悍的内置类型切片("动态数组"),与数组相比切片的长度是不固 ...

  5. MongoDB 数据库引用

    MongoDB 引用有两种: 手动引用(Manual References) DBRefs DBRefs vs 手动引用 考虑这样的一个场景,我们在不同的集合中 (address_home, addr ...

  6. PHP Libxml 函数

    PHP Libxml 简介 Libxml 函数和常量与 SimpleXML.XSLT 以及 DOM 函数一起使用. 安装 这些函数需要 Libxml 程序包. 在 xmlsoft.org 下载 PHP ...

  7. SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置

    一直对springmvc和mybatis挺怀念的,最近想自己再搭建下框架,然后写点什么. 暂时没有整合缓存,druid也没有做ip地址的过滤.Spring的AOP简单配置了下,也还没具体弄,不知道能不 ...

  8. YAML 在Python中的配置应用

    环境搭建 YAML语法 语法规则 数据结构 列表数组 原子量 YAML应用 案例 load dump 总结 YAML是一个堪比XML,JSON数据格式的更加方便,简洁的,易于人眼阅读的序列化数据格式. ...

  9. 疯狂的Django 之深度外键跨表查找之疯狂INNER JOIN

    定义Model: from django.db import models class Moreinfo(models.Model): weight = models.FloatField() hei ...

  10. Sublime Text 3下C/C++开发环境搭建

    Sublime Text 3下C/C++开发环境搭建 之前在Linux Mint 17一周使用体验中简单介绍过Sublime Text. 1.Sublime Text 3安装 Ubuntu.Linux ...