Test instructions
There are n filenames, sorted by column precedence, left-aligned output. The longest file name is the length of M, the last column is M, and the other column length is m+2.
Analysis:
The problem is very simple, but to write the code to refine, still need to think about it. LRJ code has two highlights, one is the print sub-function, and the other is the calculation of the number of rows. Experience with your heart
1#include <iostream>2#include <cstdio>3#include <string>4#include <algorithm>5 using namespacestd;6 7 Const intMAXL = -;8 Const intMAXN = -+5;9 stringNAMES[MAXN];Ten One voidPrintConst string& S,intLenCharextra) A { -printf"%s", S.c_str ()); - for(inti = S.length (); i < Len; ++i) Putchar (extra); the } - - intMain () - { + //freopen ("In.txt", "R", stdin); - intN; + while(SCANF ("%d", &n) = =1) A { at intM =0; - for(inti =0; I < n; ++i) - { -CIN >>Names[i]; -m = max (M, (int) names[i].length ()); - } inSort (names, names +n); - intcols = (maxl-m)/(M +2) +1, rows = (N-1)/cols +1; toPrint"", -,'-'); +Puts""); - for(intR =0; R < rows; ++R) the { * for(intc =0; c < cols; ++c) $ {Panax Notoginseng intindex = c * rows +R; - if(Index < N) print (Names[index], c = = cols-1? m:m+2,' '); the } +Puts""); A } the } + - return 0; $}code June
UVa 400 (water problem) Unix ls