PAT1055

2017-08-16  本文已影响0人  Fattyu

题目链接https://www.patest.cn/contests/pat-b-practise/1055
注意事项
1.按照名字升序比较时,不应该用char 的数组,而是应该用string.
2.vector 建立数组
3.最后一行的人数final=n/k+n%k;
4.循环中int i=1;?

题目代码

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct student
{
    string name;
    int heigh;
};
bool compare(struct student a,struct student b)
{
    if(a.heigh!=b.heigh)
        return a.heigh>b.heigh;
    else
    {
       return a.name<b.name;
    }
}
int main()
{
    int N,k;
    cin>>N>>k;
    vector<struct student> v(N);
    for(int i=0;i<N;i++)
    {
        cin>>v[i].name>>v[i].heigh;
    }
    sort(v.begin(),v.end(),compare);
    int col=N/k;
    int final=N/k+N%k;
    string result="";
    for(int i=0;i<N;)
    {
        if(i == 0){ //先站最后一排
            result = v[i].name;
            ++i;
            for(int j = 1; j < final; ++j, ++i){
                if(j % 2 == 1){
                    result = v[i].name + " " + result;
                }
                else{
                    result = result + " " + v[i].name;
                }
            }
            cout << result << endl;
        }
        else{
            result = v[i].name;
            ++i;
            for(int j = 1; j < col; ++j, ++i){
                if(j % 2 == 1){
                    result = v[i].name + " " + result;
                }
                else{
                    result = result + " " + v[i].name;
                }
            }
            cout << result << endl;
        }
    }
    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读