PAT乙级 1055. 集体照 (25)

来源:互联网 发布:3d大型网络手机游戏 编辑:程序博客网 时间:2024/06/03 01:42
//PAT-1-1057
//思路:按要求一行一行地排就行了,可以自己多写几种情况,检验下标的正确性#include <iostream>#include <algorithm>#include <string>#include <math.h>#include <sstream>#include <vector>using namespace std;typedef struct{    string name;    int height;}info;int comp(info a,info b){    if (a.height!=b.height) {        return a.height>b.height;    }    return a.name<b.name;}int main(){        int n,k;    cin>>n>>k;    vector<info> s;    for (int i=0; i<n; i++) {        info temp;        cin>>temp.name>>temp.height;        s.push_back(temp);    }    sort(s.begin(), s.end(), comp);    int per=n/k;    //cout<<per<<endl;    int first=per;    if (per*k<n) {        first++;    }    int count=0;    for (int i=0; i<n;count++ ) {        if (i==0) {                        if (first%2==1) {                for (int j=0; j<first/2; j++) {                                        cout<<s[2*(per/2-j)-1].name<<" ";                }                if (first>1) {                    cout<<s[0].name<<" ";                }                else cout<<s[0].name;                for (int j=0; j<first/2; j++) {                   if(j!=first/2-1) cout<<s[2*(j+1)].name<<" ";                    else cout<<s[2*(j+1)].name;                }            }else{                for (int j=0; j<first/2; j++) {                    cout<<s[2*(first/2-j)-1].name<<" ";                }                for (int j=0; j<first/2; j++) {                    if (j!=first/2-1) {                        cout<<s[2*(j)].name<<" ";                    }                    else cout<<s[2*(j)].name;                }            }            i+=first;            cout<<endl;        }else{            if (per%2==1) {                for (int j=0; j<per/2; j++) {//                    cout<<count<<endl;                    cout<<s[2*(per/2-j)-1+first+(count-1)*per].name<<" ";                }                if (per>1) {                    cout<<s[first+(count-1)*per].name<<" ";                }else cout<<s[first+(count-1)*per].name;                                for (int j=0; j<per/2; j++) {                    if(j!=per/2-1) cout<<s[2*(j+1)+first+(count-1)*per].name<<" ";                    else cout<<s[2*(j+1)+first+(count-1)*per].name;                }            }else{                for (int j=0; j<per/2; j++) {                    cout<<s[2*(per/2-j)-1+first+(count-1)*per].name<<" ";                }                for (int j=0; j<per/2; j++) {                    if(j!=per/2-1) cout<<s[2*(j)+first+(count-1)*per].name<<" ";                    else cout<<s[2*(j)+first+(count-1)*per].name;                }            }            i+=per;            cout<<endl;                    }    }    return 0;}

原创粉丝点击