首页 > 代码库 > Unix ls UVA 400
Unix ls UVA 400
说说:这道题在开始的时候理解有偏差。其实这道题把unix里的ls命令简化了,这道题的输出格式其实是输出若干列的文件名,且每列占据的字符数是最长的文件名的长度加二,当然最后一列除外,最后一列就是最长的文件名的长度。其实只要将行数从一开始不断的测试,直到输出每行的字符数不超过六十即可。但是我开始理解成要求输出的行数最少,且每列的长度为该列中的最长的文件名的长度加二。这样的话就变得复杂了许多。因为你在计算最少的行数的时候就要统计此种情况下的每列的最长字符数,从而判断每行总的字符数是否会超过六十。原本打算把这个搞出来的,不过AC之后就有点懒得写啦....嘿嘿
题目:
Unix ls
Unix ls |
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.
你工作的公司正在推广一个新的品牌的电脑并且希望同时推广一套新的类unix系统。你的任务就是写一个ls函数的格式化程序。
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
最长的文件名排成若干列。文件名包含1到60个字符并且被排版成左对齐的若干列。最右边的那一列的宽度等于最长的文件名的长度,其余各列是最长文件名的长度加二。
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
你的程序要在一行中排尽可能多的列取去填满60个字符。
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
在输出格式化的文件名之前你需要输入一行60个(-)。排序后,1~R号文件名会被列在第一列,R+1~2R的文件名会被列在第二列,依次类推。
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
源代码:
#include <stdio.h> #include <string.h> #define MAXN 100+5 #define MAXL 60+5 char file[MAXN][MAXL],word[MAXL]; int file_len[MAXN];//保存各文件名的长度 int row_num,cow_num,file_num,max_len; void word_insert(int);//获取文件名并排序 void get_row_num();//获取输出行数 void get_file_len();//获得每个文件名的长度 int main(){ int i,k,j,space; // freopen("data","r",stdin); while(~scanf("%d",&file_num)){ for(i=0;i<file_num;i++){ scanf("%s",word); word_insert(i); } get_file_len(); get_row_num(); for(i=0;i<60;i++) printf("-"); putchar('\n'); for(i=0;i<row_num;i++){ for(j=0;j<cow_num;j++) if(j*row_num+i<file_num){ space=max_len+2-file_len[j*row_num+i];//输出一个文件名后需要输出的字符数 if(j==cow_num-1)//注意最后一列的输出 space-=2; printf("%s",file[j*row_num+i]); for(k=0;k<space;k++) putchar(' '); } putchar('\n'); } } return 0; } void get_row_num(){ for(row_num=1;;row_num++){//行数从一开始测试 cow_num=file_num%row_num?file_num/row_num+1:file_num/row_num; if(cow_num*(max_len+2)-2<=60)//满足条件即可 return; } } void word_insert(int cnt){//插入文件名并排序 int i,j; if(cnt==0){ strcpy(file[0],word); return; } for(i=cnt-1;i>=0;i--) if(strcmp(file[i],word)>0) strcpy(file[i+1],file[i]); else break; strcpy(file[i+1],word); return; } void get_file_len(){ int i,j,k; max_len=0;//max_len保存最长的文件名 for(i=0;i<file_num;i++){ file_len[i]=strlen(file[i]); if(max_len<file_len[i]) max_len=file_len[i]; } return ; }