UVa - 1593 Unix ls(STL)

给你一堆文件名,排序后按列优先的方式左对齐输出。

假设最长文件名长度是M,那么每一列都要有M+2字符,最后一列有M字符。

inmanip真NB。。orz



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <cstring>
#include <iomanip>
#include <algorithm>
using namespace std;

const int MAX = 60;

int main()
{
    int n;
    while(scanf("%d", &n) != EOF)
    {
        vector<string> ss;
        int maxlen = 0;
        string s;
        for (int i = 0; i < n; i++)
        {
            cin >> s;
            maxlen = max(maxlen, (int)s.size());
            ss.push_back(s);
        }

        sort(ss.begin(), ss.end());

        int cols = (MAX+2) / (maxlen+2);
        int rows = (n-1) / cols + 1; //巧妙

        //cout << cols << " " << rows << endl;

        printf("------------------------------------------------------------\n");
        cout << setiosflags(ios :: left);
        for (int j = 0; j < rows; j++)
        {
            for (int i = 0; i < n; i++)
                if (i % rows == j) //SB的我改了好久
                    cout << setw(maxlen + 2) << ss[i];
            cout << endl;
        }
    }

}

猜你喜欢

转载自www.cnblogs.com/ruthank/p/9020276.html