The computer company you work for is introducing a brand new computer line and is developing a new Unix-like operating system to be introduced along with the new computer. Your assignment is to write the formatter for the ls function.

Your program will eventually read input from a pipe (although for now your program will read from the input file). Input to your program will consist of a list of (F) filenames that you will sort (ascending based on the ASCII character values) and format into (C) columns based on the length (L) of the longest filename. Filenames will be between 1 and 60 (inclusive) characters in length and will be formatted into left-justified columns. The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. There will be as many columns as will fit in 60 characters. Your program should use as few rows (R) as possible with rows being filled to capacity from left to right.

Input

The input file will contain an indefinite number of lists of filenames. Each list will begin with a line containing a single integer (  ). There will then be N lines each containing one left-justified filename and the entire line's contents (between 1 and 60 characters) are considered to be part of the filename. Allowable characters are alphanumeric (a to zA to Z, and 0 to 9) and from the following set { ._- } (not including the curly braces). There will be no illegal characters in any of the filenames and no line will be completely empty.

Immediately following the last filename will be the N for the next set or the end of file. You should read and format all sets in the input file.

Output

For each set of filenames you should print a line of exactly 60 dashes (-) followed by the formatted columns of filenames. The sorted filenames 1 to R will be listed down column 1; filenames R+1 to 2R listed down column 2; etc.

Sample Input

10
tiny
2short4me
very_long_file_name
shorter
size-1
size2
size3
much_longer_name
12345678.123
mid_size_name
12
Weaser
Alfalfa
Stimey
Buckwheat
Porky
Joe
Darla
Cotton
Butch
Froggy
Mrs_Crabapple
P.D.
19
Mr._French
Jody
Buffy
Sissy
Keith
Danny
Lori
Chris
Shirley
Marsha
Jan
Cindy
Carol
Mike
Greg
Peter
Bobby
Alice
Ruben

Sample Output

------------------------------------------------------------
12345678.123 size-1
2short4me size2
mid_size_name size3
much_longer_name tiny
shorter very_long_file_name
------------------------------------------------------------
Alfalfa Cotton Joe Porky
Buckwheat Darla Mrs_Crabapple Stimey
Butch Froggy P.D. Weaser
------------------------------------------------------------
Alice Chris Jan Marsha Ruben
Bobby Cindy Jody Mike Shirley
Buffy Danny Keith Mr._French Sissy
Carol Greg Lori Peter 这道题的关键点在于sort排序,还有输出空格的函数,以及行数和列数的计算。
题目要求按列优先输出,这一点可以通过“当前列数*总行数+当前行数”来定位。
#include<iostream>
#include<string>
#include<algorithm>
using namespace std; const int maxcol=;
const int maxn=;
string filenames[maxn]; //输出字符串s,长度不足len时补字符extra
void print(const string& s,int len,char extra){
cout<<s;
for(int i=;i<len-s.length();i++)
cout<<extra;
} int main(){
int n;
while(cin>>n){
int m=;
for(int i=;i<n;i++){
cin>>filenames[i];
m=max(m,(int)filenames[i].length());//STL的max
}
//计算列数cols和行数rows
int cols=(maxcol-m)/(m+)+;
int rows=(n-)/cols+;
print("",,'-');
cout<<"\n";
sort(filenames,filenames+n);//排序
for(int r=;r<rows;r++){
for(int c=;c<cols;c++){
int idx=c*rows+r;
if(idx<n)
print(filenames[idx],c==cols-?m:m+,' ');
}
cout<<"\n";
}
}
return ;
}

UVa400 Unix is的更多相关文章

  1. UVa400.Unix ls

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

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

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

  3. Unix&Linux技术文章目录(2015-12-22更新)

    Unix & Linux 方面的博客整理.归纳分类,要坚持不懈的学习Unix &Linux,加油!技术需要累积和沉淀.更需要锲而不舍的精神.持之以恒的毅力!借此下面名句勉励自己! 书上 ...

  4. C#中DateTime.Ticks属性及Unix时间戳转换

    1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...

  5. 《UNIX环境高级编程》笔记——3.文件IO

    一.引言 说明几个I/O函数:open.read.write.lseek和close,这些函数都是不带缓冲(不带缓冲,只调用内核的一个系统调用),这些函数不输入ISO C,是POSIX的一部分: 多进 ...

  6. 《UNIX环境高级编程》笔记——2.标准和实现

    随着UNIX各种衍生版本不断发展壮大,标准化工作就十分必要.其实干啥事都是这样,玩的人多了,必须进行标准化. 一.UNIX标准 1.1 ISO C(ANSI C) ANSI:Amerocan Nato ...

  7. 《UNIX环境高级编程》笔记——1.UNIX基础知识

    这一章节侧重一些基本概念和书中用到的一些名词. 一.引言 所有的操作都提供服务,典型的服务包括:执行新程序.打开文件.读写文件.分配存储区以及获得当前时间等. 二.UNIX体系结构 其实linux常见 ...

  8. UNIX下的LD_PRELOAD环境变量

    UNIX下的LD_PRELOAD环境变量 也许这个话题并不新鲜,因为LD_PRELOAD所产生的问题由来已久.不过,在这里,我还是想讨论一下这个环境变量.因为这个环境变量所带来的安全问题非常严重,值得 ...

  9. Unix网络单词汇总

    Chrome开发者工具 Elements(元素).Network(网络).Sources(源代码:调试JS的地方).Timeline(时间线).Profiles(性能分析).Resources(资源: ...

随机推荐

  1. eclipse 建立Maven java工程

    1.在项目资源管理器右键---新建---项目 2.在选择向导里选择Maven---Maven Project 3.选择默认的工作空间,下一步 4.选择箭头所示选项 5.输入组织名和工程名.点击完成

  2. [bzoj1892][bzoj2384][bzoj1461][Ceoi2011]Match/字符串的匹配_KMP_树状数组

    2384: [Ceoi2011]Match 1892: Match 1461: 字符串的匹配 题目大意: 数据范围: 题解: 很巧妙的一道题呀. 需要对$KMP$算法有很深的理解才行. 首先我们需要发 ...

  3. 查询统计SQL分组求和使用小技巧

    我们在做查询统计时,肯定会遇到将查询结果再次分组求和这种需求,但是往往查询的sql本身就比较复杂,再使用分组函数不太可能,那么这时候我们就想到了用临时表的办法,通过联合临时表我们就可以获得想要的分组求 ...

  4. hadoop(一HDFS)

    hadoop(一HDFS) 介绍 狭义上来说: hadoop指的是以下的三大系统: HDFS :分布式文件系统(高吞吐,没有延时要求,容错性,扩展能力) MapReduce : 分布式计算系统 Yar ...

  5. 前缀和&二维前缀和

    我们知道,数组上的前缀和S[i]=S[i-1]+a[i] 那么,怎样求二维前缀和呢? 二维前缀和: 绿色点的前缀和就是黄色.红色.灰色和绿色的点权和 怎样计算? s[i][j]=s[i-1][j]+s ...

  6. 从入门到自闭之Python--RESTful API规范与序列化

    RESTful API规范 REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fieldi ...

  7. Redis迁移键

    迁移键: move key db 用于在Redis内部进行数据迁移 dump key + restore key ttl value 可以实现在不同的Redis实例之间进行数据迁移 127.0.0.1 ...

  8. Qt 4.5发布(最大的变动是换用LGPL许可证,移植进了苹果的Cocoa框架。之前的Qt只支持Carbon框架,现在的Qt 4.5两者都支持。单一源代码创建出支持32位或64位字节的Intel或PowerPC Mac二进制文件)

            Nokia的开源Qt开发工具正式发布了4.5版.如前所述,Qt 4.5最大的变动是换用LGPL许可证,目前采用的三个许可证分别为LGPL/GPL和商业许可证.           新版 ...

  9. Ef数据GroupBy多字段查询Vb.net与c#参考

    Dim g = lst.Data.GroupBy(Function(T) New With { Key T.mName, Key T.mUnit, Key T.mPrice }).Select(Fun ...

  10. Update导致SQL Server死锁的典型方法(转载)

    此文为转载文章,描述的很好,没有验证过. 最近遇到了一个看上去很奇怪,分析起来很有意思的死锁问题.这个死锁看上去难以理解.而分析过程中,又使用了很多分析SQL Server死锁的典型方法.记录下来整个 ...