Note: At the beginning, there was a deviation in understanding this question. In fact, this question simplifies the LS command in Unix. The output format of this question is actually to output the file names of several columns, the maximum number of characters occupied by each column is the length of the file name plus two. Except for the last column, the last column is the length of the longest file name. In fact, you only need to test the number of lines from the beginning until the number of characters in each line cannot exceed 60. However, I began to understand that the number of output rows is the least, and the length of each column is the length of the longest file name in the column plus two. In this way, it becomes much more complicated. When calculating the minimum number of rows, You need to count the maximum number of characters in each column in this case to determine whether the total number of characters in each row will exceed 60. I was going to make it out, but AC is a little too lazy to write it...
Question:
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 forLsFunction.
The company you work for is promoting a new brand of computer and wants to promote a new Unix-like system at the same time. Your task is to write a formatting program 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 (F) Filenames
Your program will eventually read data from the pipeline (although your program now reads data from the input file ). Input to your program is a series of file names, you need to sort them (similar to the lexicographically) and follow
That you will sort (ascending based on the ASCII character values) and format (C) Columns Based on the length (L) Of the longest filename. filenames will be between 1
The longest file names are arranged in several columns. The file name contains 1 to 60 characters and is formatted as left-aligned columns. The width of the rightmost column is equal to the length of the longest file name. The length of the longest file name is added to the length of the other columns.
And 60 (aggressive) 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 your columns as will fit in 60 characters. your program shocould use as few rows (R) As possible with rows
Your program needs to arrange as many columns as possible in a row to fill up with 60 characters.
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 beNLines 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 (AToZ,AToZ, And0To9) 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 beNFor the next set or the end of file. You shoshould read and format all sets in the input file.
Output
For each set of filenames you shoshould print a line of exactly 60 dashes (-) Followed by the formatted columns of filenames. The sorted filenames 1RWill be listed down
Before outputting formatted file names, you must enter 60 (-) entries in a row (-). After sorting, 1 ~ The r file name will be listed in the first column, R + 1 ~ The 2R file name will be listed in the second column, and so on.
Column 1; filenamesR+ 1 to 2RListed down Column 2; etc.
Sample Input
10tiny2short4mevery_long_file_nameshortersize-1size2size3much_longer_name12345678.123mid_size_name12WeaserAlfalfaStimeyBuckwheatPorkyJoeDarlaCottonButchFroggyMrs_CrabappleP.D.19Mr._FrenchJodyBuffySissyKeithDannyLoriChrisShirleyMarshaJanCindyCarolMikeGregPeterBobbyAliceRuben
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
Source code:
# Include <stdio. h> # include <string. h> # define maxn 100 + 5 # define maxl 60 + 5 char file [maxn] [maxl], word [maxl]; int file_len [maxn]; // Save the length of each file name: int row_num, cow_num, file_num, max_len; void word_insert (INT); // obtain the file name and sort void get_row_num (); // obtain the output row void get_file_len (); // obtain the length of each file name 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]; // output the number of characters to be output after a file name if (j = cow_num-1) // note the output space in the last column = 2; printf ("% s ", file [J * row_num + I]); For (k = 0; k <space; k ++) putchar ('');} Pu Tchar ('\ n') ;}} return 0;} void get_row_num () {for (row_num = 1; row_num ++) {// test the number of rows from the beginning: cow_num = file_num % row_num? File_num/row_num + 1: file_num/row_num; If (cow_num * (max_len + 2)-2 <= 60) // return if the conditions are met;} void word_insert (int cnt) {// Insert the file name and sort 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 stores the longest file name 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 ;}