400:Unix ls

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37754288/article/details/79981064

Unix ls

 
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int len = 60 + 2;
void print(const string& s,int len,char extra){
    cout<<s;
    for(int i = 0;i < len - s.size();i++) cout<<extra;
}
int main(){
    int n;
    while(cin>>n){
        string s;
        int maxlen = 0;
        vector<string>file;
        for(int i = 0;i < n;i++){
            cin>>s;
            maxlen = max(maxlen,(int)s.size()); // (int)
            file.push_back(s);
        }
        sort(file.begin(),file.end());
        int per = len / (maxlen+2),row = n / per;
        if(n % per) row += 1;
        print("-",60,'-');
        cout<<endl;
        for(int i = 0;i < row;i++){
            for(int j = 0;j < per;j++){
                int index = j*row + i;
                if(index < n) print(file[index],c == per-1 ? maxlen : maxlen + 2,' ');
            }
            cout<<endl;
        }
    }
    return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页