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

  1. 10
  2. tiny
  3. 2short4me
  4. very_long_file_name
  5. shorter
  6. size-1
  7. size2
  8. size3
  9. much_longer_name
  10. 12345678.123
  11. mid_size_name
  12. 12
  13. Weaser
  14. Alfalfa
  15. Stimey
  16. Buckwheat
  17. Porky
  18. Joe
  19. Darla
  20. Cotton
  21. Butch
  22. Froggy
  23. Mrs_Crabapple
  24. P.D.
  25. 19
  26. Mr._French
  27. Jody
  28. Buffy
  29. Sissy
  30. Keith
  31. Danny
  32. Lori
  33. Chris
  34. Shirley
  35. Marsha
  36. Jan
  37. Cindy
  38. Carol
  39. Mike
  40. Greg
  41. Peter
  42. Bobby
  43. Alice
  44. Ruben

Sample Output

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

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. day31 socket套接字编程

    为什么要有套接字编程? 在上节课的学习中,我们学习了OSI七层协议,但是如果每次进行编程时我们都需要一层一层的将各种协议使用在我们的程序中,这样编写程序实在是太麻烦了,所以为了让程序的编写更加的简单, ...

  2. GitHub 将源代码保存在北极洞穴,至少使用 1000 年!

    最近,GitHub分享了开放Arctic Code Vault的计划,该计划旨在存储和保存Flutter和TensorFlow等开源软件. 所有开放源代码项目的代码都将存储在胶片上,该胶片每帧包含88 ...

  3. laravel框架之自帶登錄&註冊

    //控制器層 <?php namespace App\Http\Controllers\admin; use App\Models\admin\Users; use Illuminate\Htt ...

  4. MySql 多表关系

    多表关系 一对一关系 一对一关系是最好理解的一种关系,在数据库建表的时候可以将人表的主键放置与身份证表里面,也可以将身份证表的主键放置于人表里面 一对多关系 班级是1端,学生是多端,结合面向对象的思想 ...

  5. T100——动态更改Label的说明

    例子: #設定科目名稱    IF g_prog = 'aapt300' THEN       CALL cl_set_comp_att_text("lbl_apca036",cl ...

  6. 怎样设置 MySQL 远程连接

    允许用户 root 在 任何IP 上都可以远程连接 所有 mysql数据库 并具有操作数据库的 所有权限, 密码为: myPassword mysql -u root -p grant all PRI ...

  7. 怎样解决在执行 vue init 时提示 "vue : 无法加载文件" 的问题?

    注意, 以下操作需要 以管理员身份 在 PowerShell 中进行, 不能是 CMD / Git Bash 等. 1. 以 管理员身份 运行 PowerShell 2. 执行 get-Executi ...

  8. 【原创】大叔经验分享(73)scala akka actor

    import java.util.concurrent.{ExecutorService, Executors, TimeUnit} import akka.actor.{Actor, ActorSy ...

  9. springboot-oracle工程win下正常,centos下不能访问数据库

    工程在win下正常运行,部署到centos下出现下述异常: ### Error querying database. Cause: org.springframework.jdbc.CannotGet ...

  10. JavaScript的常用浏览器设置

    用什么浏览器?如果您不告诉我您使用的浏览器,我将告诉您有关JavaScript的常用浏览器设置.~火狐在菜单栏中选择工具->选项->内容以查看启用javascript的选项.Interne ...