Unix ls

Time Limit:                                                        3000MS                           Memory Limit: Unknown   64bit IO Format:                            %lld & %llu                        

Submit                                        Status

Description

 

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 thels 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 beN 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 toz, 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 toR 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

题意:输入N后有N段字符串,读入进行按字典序排序,然后按尽量少行数输出,每行最多60个字符,因此最大列数colum=(60-maxlen)/(maxlen+2)+1;当然maxlen最长字符串的长度,行数row=(int)(ceil( (double)t/colum) )。然后最后一列的每个字符串输出占位数为maxlen,其他为maxlen+2。题意刚开始看不懂,直接百度了。

代码:

#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstdio>
#include<set>
#include<sstream>
#include<map>
#include<vector>
#include<iomanip>
using namespace std;
bool cmp(const string &a,const string &b)
{
return a<b;
}
int main(void)
{
int t,maxlen,colum,row;
while (cin>>t)
{
string list[1000];
maxlen=0;
for (int q=0; q<t; q++)
{
cin>>list[q];
maxlen=max(maxlen,(int)list[q].size());//计算字符串最大长度
}
sort(list,list+t,cmp);//字典序排序
colum=(60-maxlen)/(maxlen+2)+1;//计算列数
row=(int)(ceil((double)t/colum));//计算行数
cout<<"------------------------------------------------------------"<<endl;
for (int i=0; i<row; i++)//行外循环
{
for (int j=0; j<colum; j++)//列内循环
{
if(j<colum-1)
cout<<left<<setw(maxlen)<<list[j*row+i]<<" ";//不是最后一列,maxlen再多输俩空格
else
cout<<left<<setw(maxlen)<<list[j*row+i];//按maxlen输出
}
cout<<endl;
}
}
return 0;
}

UVa——400Unix ls(字典序文本处理输出iomanip)的更多相关文章

  1. ls按时间排序输出文件列表

    文件转自:http://www.2cto.com/os/201303/197829.html ls按时间排序输出文件列表   首先,ls --help查看ls相关的与时间排序相关的参数:   > ...

  2. 【HTML5游戏开发小技巧】RPG情景对话中,令文本逐字输出

    以前用JAVAscript实现过令文本逐字输出的效果,今天我来用html5中的canvas实现一下.canvas里的内容可不像<p>那样好操作,首先,你需要懂得一些html5的API才能操 ...

  3. 2019.11.13课堂实验之用Linux下的shell脚本完成两文本交替输出

    有两个文本如下,实际中并不知道两文本各有多少行: 文本1.txt aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ccccccccccccccccccccccccccc ...

  4. Linux终端和win32控制台文本颜色输出

    在使用putty.secureCRT.XShell等终端仿真器连接linux系统时,ls.vim等工具的输出都含有各种颜色,这些颜色的输出大大地增强了文本的可读性. 通常我们可以使用echo命令加-e ...

  5. 【Windows编程】系列第三篇:文本字符输出

    上一篇我们展示了如何使用Windows SDK创建基本控件,本篇来讨论如何输出文本字符. 在使用Win32编程时,我们常常要输出文本到窗口上,Windows所有的文本字符或者图形输出都是通过图形设备接 ...

  6. 转:MVC2表单验证失败后,直接返回View,已填写的内容就会清空,可以这样做;MVC2输出文本;MVC2输出PDF文件

    ViewData.ModelState.AddModelError("FormValidator", message); foreach (string field in Requ ...

  7. api (三)文本字符输出 (转)

    在使用Win32编程时,我们常常要输出文本到窗口上,Windows所有的文本字符或者图形输出都是通过图形设备接口(GDI)进行的,Windows的三大核心组件之一的GDI32.dll封装了所有的文本和 ...

  8. SAM I AM UVA - 11419 最小点集覆盖 要输出具体覆盖的行和列。

    /** 题目:SAM I AM UVA - 11419 链接:https://vjudge.net/problem/UVA-11419 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一 ...

  9. Log4net系列一:Log4net搭建之文本格式输出【转】

    前言 项目开发中,记录项目日志是必须的,如果非要说日志的重要性(日志可看做,飞机的黑匣子,或者汽车的行车记录仪),根据等级进行记录,方便我们排查相关问题,以后项目运维中,也方便很多.基本上我们进入一家 ...

随机推荐

  1. 将Java应用部署到SAP云平台neo环境的两种方式

    方法1 - 使用Eclipse Eclipse里新建一个服务器: 服务器类型选择SAP Cloud Platform: 点Finish,成功创建了一个Server: Eclipse里选择要部署的项目, ...

  2. cv2.Laplacian 模糊判断

    简介   cv2.Laplacian是用来判断图像模糊度的     函数原型 dst = cv2.Laplacian(src, ddepth[, dst[, ksize[, scale[, delta ...

  3. 2018.3.3 多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析)

    多线程中继承Thread 和实现Runnable接口 的比较(通过售票案例来分析) 通过Thread来实现 Test.java package com.lanqiao.demo4; public cl ...

  4. 数据库-SQL语法:把一个字段的值设为随机整数

     update test2 set zuig = (cast ( ceiling (rand()*9) as int))  

  5. skynet 学习笔记-netpack模块(1)

    int luaopen_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop } ...

  6. 【贪心 计数 倍增】bzoj4458: GTY的OJ

    倍增写挂调了半个晚上 Description 身为IOI金牌的gtyzs有自己的一个OJ,名曰GOJ.GOJ上的题目可谓是高质量而又经典,他在他的OJ里面定义了一个树形的分类目录,且两个相同级别的目录 ...

  7. 到底该如何理解DevOps这个词

    炒了8年的概念,到底该如何理解DevOps这个词? 转载本文需注明出处:EAII企业架构创新研究院,违者必究.如需加入微信群参与微课堂.架构设计与讨论直播请直接回复公众号:“EAII企业架构创新研究院 ...

  8. centos 安装 python3 分类链接

    上一篇文章描述了如何安装python3,但是在后续安装pip便不断报出缺少各类模块,安装一个又需要依赖另一个,导致安装过程非常繁琐.究其原因,我是安装centos-minimal版本,有许多功能不是完 ...

  9. PHP数组函数 array_multisort() ----对多个数组或多维数组进行排序

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  10. 循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案

    在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息.我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息.在循环字典的时候,当用户id和 ...