UVa400 Unix is
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 z, A 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的更多相关文章
- UVa400.Unix ls
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 400 - Unix ls (Unixls命令)
csdn : https://blog.csdn.net/su_cicada/article/details/86773007 例题5-8 Unixls命令(Unix ls,UVa400) 输入正整数 ...
- Unix&Linux技术文章目录(2015-12-22更新)
Unix & Linux 方面的博客整理.归纳分类,要坚持不懈的学习Unix &Linux,加油!技术需要累积和沉淀.更需要锲而不舍的精神.持之以恒的毅力!借此下面名句勉励自己! 书上 ...
- C#中DateTime.Ticks属性及Unix时间戳转换
1.相关概念 DateTime.Ticks:表示0001 年 1 月 1 日午夜 12:00:00 以来所经历的 100 纳秒数,即Ticks的属性为100纳秒(1Ticks = 0.0001毫秒). ...
- 《UNIX环境高级编程》笔记——3.文件IO
一.引言 说明几个I/O函数:open.read.write.lseek和close,这些函数都是不带缓冲(不带缓冲,只调用内核的一个系统调用),这些函数不输入ISO C,是POSIX的一部分: 多进 ...
- 《UNIX环境高级编程》笔记——2.标准和实现
随着UNIX各种衍生版本不断发展壮大,标准化工作就十分必要.其实干啥事都是这样,玩的人多了,必须进行标准化. 一.UNIX标准 1.1 ISO C(ANSI C) ANSI:Amerocan Nato ...
- 《UNIX环境高级编程》笔记——1.UNIX基础知识
这一章节侧重一些基本概念和书中用到的一些名词. 一.引言 所有的操作都提供服务,典型的服务包括:执行新程序.打开文件.读写文件.分配存储区以及获得当前时间等. 二.UNIX体系结构 其实linux常见 ...
- UNIX下的LD_PRELOAD环境变量
UNIX下的LD_PRELOAD环境变量 也许这个话题并不新鲜,因为LD_PRELOAD所产生的问题由来已久.不过,在这里,我还是想讨论一下这个环境变量.因为这个环境变量所带来的安全问题非常严重,值得 ...
- Unix网络单词汇总
Chrome开发者工具 Elements(元素).Network(网络).Sources(源代码:调试JS的地方).Timeline(时间线).Profiles(性能分析).Resources(资源: ...
随机推荐
- APP安全_Android反编译
反编译 Android的反编译工具:apktool,JEB等. Apk 文件的结构,如下: META-INF:签名文件 res:资源文件,里面的 xml 格式文件在编译过程中由文本格式转化为二进制的 ...
- Stream系列(八)Reduce方法使用
裁减计算 视频讲解: https://www.bilibili.com/video/av77715582/ EmployeeTest.java package com.example.demo; i ...
- Java中简单测试FastDFS的文件上传
pom.xml文件内容如下: <dependencies> <!-- fastdfs --> <dependency> <groupId>org.cso ...
- 【转帖】k8s-kubectl命令大全
https://www.cnblogs.com/fuyuteng/p/9458282.html 学习一下 Kubectl命令行管理对象 类型 命令 描述 基础命令 create 通过文件名或标准输入创 ...
- oracle——学习之路(oracle内置函数)
oracle与很多内置函数,主要分为单行函数与集合函数. 首先要提一下dual表,它oracle的一个表,没有什么实质的东西,不能删除它,否则会造成Oracle无法启动等问题,他有很大用处,可以利用它 ...
- HTML 标签的 for 属性
HTML 标签的 for 属性 for 属性规定 label 与哪个表单元素绑定. 隐式和显式的联系 label通常以下面两种方式中的一种来和表单控件相联系: 将表单控件作为标记标签的内容,这样的就是 ...
- Docker——四种网络模式
docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: bridge模式:使用–net =bridge指定,默认设置: host模式 ...
- Java反射理解(五)-- 方法反射的基本操作
Java反射理解(五)-- 方法反射的基本操作 方法的反射 1. 如何获取某个方法 方法的名称和方法的参数列表才能唯一决定某个方法 2. 方法反射的操作 method.invoke(对象,参数列表) ...
- Lua访问网页
示例 例子,实现https方式,登录网站,访问某个网页,修改其中参数的功能.其中xx应用时候需要修改. require("curl") local ipList = { " ...
- stuff拼接字符串
stuff stuff(param1,startIndex,length,param2) 说明:将param1中自startIndex(SQL中都是从1开始,而非0)起,删除length个字符,然后用 ...