UVA-400 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 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.

 

题目链接:https://vjudge.net/problem/UVA-400

 

思路

水题,模拟就行。

但是自己做完做这道题之后,又和书上的对比对比,差距是真的大。

string类的length()或者size()函数返回的是unsigned integer(无符号数)类型。而用在for循环时,正常不会出错,但作为判断条件时,当s.length()等于0时,s.length()-1 不等于 -1

所以在进行比较时,需要将他转型为int。

另外,切记,用s.length()或者s.size()做运算时,也要注意上边那一点。

 

还有,刘老的那一行rows = (n-1) / cols +1; 太妙了

 

我还是太菜了,加油吧!

代码

我自己AC的代码

#include
#include
#include
using namespace std;

const int maxn = 10000 + 10;
const int colSize = 60;

void show(string a[],int cols,int rows,int M,int n){

    int len;
    for(int k=0;k= n){
                break;
            }
            cout<>n){

        for(int i=0;i<60;i++){
            cout<<"-";
        }
        cout<>a[i];
            int p = a[i].size();
            m = max(m,p);
        }

        sort(a,a+n);

        int cols = (colSize-m)/(m+2)+1;
        int rows = (n%cols)? n/cols+1 : n/cols;

        show(a,cols,rows,m,n);
    }
    return 0;
}

 

书上的代码:求rows那一段真的是很妙

#include
#include
#include
using namespace std;

const int maxn = 10000 + 10;
const int maxcol = 60;

void print(const string& a,int len,char e){

    cout<>n){

        int M = 0;
        for(int i=0;i < n;i++){
            cin >> a[i];
            M = max(M,(int)a[i].size());
        }

        int cols = (maxcol-M)/(M+2) + 1,rows = (n-1)/cols + 1;

        print("",60,'-');
        cout<<"\n";

        sort(a,a+n);

        for(int i=0;i

 

你可能感兴趣的:(#,水题,c,uva,algorithm)