美文网首页工作生活
PAT 1055 集体照 (25 分)

PAT 1055 集体照 (25 分)

作者: 昭明ZMing | 来源:发表于2019-07-02 23:12 被阅读0次
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    struct node {
        string name;
        int height;
    };
    int cmp(struct node a, struct node b) {
        return a.height != b.height ? a.height > b.height : a.name < b.name;
    }
    int main() {
        int n, k, m;
        cin >> n >> k;
        vector<node> stu(n);
        for(int i = 0; i < n; i++) {
            cin >> stu[i].name;
            cin >> stu[i].height;
        }
        sort(stu.begin(), stu.end(), cmp);
        int t = 0, row = k;
        while(row) {
            if(row == k) {
                m = n/k+n%k; 
            } else {
                m = n / k;
            }
            vector<string> stemp(m);
            stemp[m / 2] = stu[t].name;
            // 左边一列
            int j = m / 2 - 1;
            for(int i = t + 1; i < t + m; i = i + 2)
                stemp[j--] = stu[i].name;
            // 右边一列
            j = m / 2 + 1;
            for(int i = t + 2; i < t + m; i = i + 2)
                stemp[j++] = stu[i].name;
            // 输出当前排
            cout << stemp[0];
            for(int i = 1; i < m; i++)
                cout << " " << stemp[i];
            cout << endl;
            t = t + m;
            row--;
        }
        return 0;
    }
    

    相关文章

      网友评论

        本文标题:PAT 1055 集体照 (25 分)

        本文链接:https://www.haomeiwen.com/subject/ckvxhctx.html