zoukankan      html  css  js  c++  java
  • PAT A1109 Group Photo [模拟]

    题目描述

    链接
    排方阵

    • 每一排的人高于下一排的人
    • 先递减排列,然后先排中间m/2+1,再排左边,然后排右边
    • 相同身高按照字母顺序递增

    分析

    • 搞错了一件事!!就是中间的人和旁边的身高相同的话,不用按字母排
    • 二维数组排序的话,可以用一维数组排好序,再放进二维数组里面,所以!!最好用vector
    • 一定要记住,如果下标从1开始,sort的写法!!!!
    • struct里面有string的话,最好用vector,不要用maxn了!!!否则struct里面就用char

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    
    const int maxn = 1e4+10;
    int a[maxn][maxn];
    
    struct node{
        char s[30];
        int num;
    }nodes[maxn];
    
    int n,k;
    bool cmp(node a, node b){
        if(a.num != b.num) return a.num > b.num;
        return strcmp(a.s, b.s) < 0;
    }
    
    int main(){
        cin>>n>>k;
        for(int i=1;i<=n;i++){
            cin>>nodes[i].s>>nodes[i].num;
        }
        sort(nodes+1,nodes+n+1,cmp);
    
        int j = 1;
        int m;
        for(int i=1;i<=k;i++){ //k行
            if(i==1) m = n % k + n/k;
            else m = n/k;
            a[i][m/2+1] = j++;
            int cnt = 1;
            while(cnt <= m/2){
                if(m/2+1-cnt>=1)
                    a[i][m/2+1-cnt] = j++;
                if(m/2+1+cnt<=m){
                    a[i][m/2+1+cnt] = j++;
                }
                cnt++;
            }
        }
        for(int i=1;i<=k;i++){
            if(i==1) m = n % k + n/k;
            else m = n/k;
            for(int j=1;j<=m;j++){
                if(j==1) cout<<nodes[a[i][j]].s;
                else cout<<" "<<nodes[a[i][j]].s;
            }
            cout<<endl;
        }
    
  • 相关阅读:
    flask-离线脚本、with在上下文的应用
    websocket原理及实时投票
    微信消息的推送
    Django + Uwsgi + Nginx 的生产环境部署2
    UVA 11853 Paintball
    UVA 12171 Sculpture
    UVA 10305 Ordering Tasks
    UVA 816 Abbott's Revenge
    UVA 699 The Falling Leaves
    UVA 12657 Boxes in a Line
  • 原文地址:https://www.cnblogs.com/doragd/p/11459019.html
Copyright © 2011-2022 走看看