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 (1≤N≤100). 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

HINT

题目大意:输入给定数量的文件名,按照字典顺序排序,按照列优先,输出。保证行数最小。

题目难点

  1. 如何计算行数和列数?

    这个直接看代码里面的公式就好了,一看就懂。

  2. 如何输出?

    对于文件名数组来说,每一次输出都要计算好输出的坐标,应当采用二层循环来实现。另外,每一个文件名达不到最大长度的使用预先初始化好的字符数组,输出前M-len位就好。

注意点:memset()头文件在csting中;sort()在algorithm中

Aceepted

#include<iostream>
#include<vector>
#include<cstring>
#include <algorithm> using namespace std; int main()
{
int sum; //文件名总数
char arr[60];
memset(arr, ' ', 60); //输出空格
while (cin >> sum)
{
string s;
vector<string>filenames; //存储文件名
int M = 0; //记录最长文件名长度
for (int i = 0;i < sum;i++)
{
cin >> s;
filenames.push_back(s); //录入文件名
if (M < s.length())M =s.length();//记录最长文件名长度
}
sort(filenames.begin(), filenames.end());//排序
cout << "------------------------------------------------------------" << endl;
int c = (60 - M) / (M + 2) + 1;//计算列数
int r = (sum - 1) / c + 1; //计算行数
for (int i = 0;i < r;i++) //行号
{
for (int j = 0 ;j <c;j++) //列号
{
int k = j * r + i; //对应的数组内部的编号
if (k >= sum)break;
if (j)cout << " "; //补全两个空格
cout << filenames[k]; //输出文件名
arr[M - filenames[k].length()] = '\0';//输出和最长文件差的字符数
cout << arr;
arr[M - filenames[k].length()] = ' ';
}
cout << endl; //输出空格
}
}
}

最新文章

  1. 【leetcode】House Robber
  2. Effective Python2 读书笔记2
  3. 关于Qt
  4. Largest Number——LeetCode
  5. sql server 存储过程、事务,增删改
  6. javascript中关于this指向问题详解
  7. kafka集群搭建(windows环境下)
  8. python self
  9. 02_HTML5+CSS详解第二天
  10. Tomcat 8项目无法启动,无报错
  11. 关于11G DataGuard 日志传输的案例
  12. PCIE错误分析
  13. Js 常用调试的方法
  14. 分布式系统监视zabbix讲解五之web监控--技术流ken
  15. A.01.02—模块的输入—高端输入
  16. [JS] ECMAScript 6 - Set &amp; Map : compare with c#
  17. 【python】pandas display选项
  18. CLR关于语言文化的类型一CultureInfo类和字符串与线程的关联
  19. [Error: Failed to find &#39;ANDROID_HOME&#39; environment variable. Try setting setting it manually
  20. 对称点line

热门文章

  1. mbatis中的#{}和${}
  2. http server源码解析
  3. Django Admin后台管理功能使用+二次开发
  4. Vuex入门、同步异步存取值进阶版
  5. SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp
  6. Wireshark使用记录
  7. [Java Tutorial学习分享]接口与继承
  8. ELK----elasticsearch7.10.1安装配置
  9. JavaScript实现动态添加员工
  10. CentOS安装libxml2报undefined reference to `gzopen64&#39;