uva400 Unix ls

vj  uva

一行的长度为60个字符, 根据最长的字符串长度算每行能放几个  再根据列数算行数  然后排个序, 输出就好了 (c*row+r)

这题的话, 把题意看懂 细节处理好 就能过了(注意每行末尾要换行, 多组输入啥的)

#include<bits/stdc++.h>

using namespace std;

#define _for(i,a,b) for(int i = (a); i < (b); i++)
#define _rep(i,a,b) for(int i = (a); i <= (b); i++)
#define all(v) (v).begin(), (v).end()

int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    int n;
    while(cin >> n) {
        int m = 0;
        vector<string> file(n);
        _for(i,0,n) {
            cin >> file[i];
            m = max(m, (int)file[i].size());
        }
        sort(all(file));
        int cols = (60-m)/(m+2)+1, rows = (n-1)/cols+1;
//cout << " m = " << m << " cols = " << cols << "  rows = " << rows << "
";
        _for(i,0,60) cout << "-";
        cout << "
";
        //_for(i,0,n) cout << file[i] << "	"; cout << "
";
        _for(j,0,rows) {
            _for(i,0,cols)
            {
                int x = i*rows+j;
                if(x >= n) continue;
                cout << file[x];
                int y = (int)file[x].size();
                _for(k,y,m) cout << " ";
                cout << (i==cols-1? "" : "  ");
            }    
            cout << "
";
        }
    }
    
    return 0;
}
原文地址:https://www.cnblogs.com/163467wyj/p/12953841.html