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<iostream>
#include<string>
#include<algorithm>
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<rows;k++){
        for(int i=0;i<cols;i++){
            if(rows*i+k >= n){
                break;
            }
            cout<<a[rows*i+k];
            len = a[rows*i+k].size();
            if(i == cols-1){
                len += 2;
            }
            for(int j=0;j<M+2-len;j++){
                cout<<" ";
            }
        }
        cout<<endl;
    }
}
string a[maxn];
int main(){
    int n;
    while(cin>>n){

        for(int i=0;i<60;i++){
            cout<<"-";
        }
        cout<<endl;

        int m = 0;
        for(int i=0;i<n;i++){
            cin>>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<iostream>
#include<string>
#include<algorithm>
using namespace std;

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

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

    cout<<a;
    for(int k=0;k < len-(int)a.size();k++){
        cout<<e;
    }

}
string a[maxn];
int main(){
    int n;
    while(cin>>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<rows;i++){
            for(int j=0;j<cols;j++){
                int idx = i+j*rows;
                if(idx < n){
                    print(a[idx],j == cols-1 ? M:M+2,' ');
                }
            }
            cout<<"\n";
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/yjz6/p/9799612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值