注意:
1、排序问题,先按照身高降序,后按名字升序
2、左右排列问题,偶数应该是:4、2、1、3;奇数应该为:4、2、1、3、5
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct person{
string name;
int height;
};
bool cmp(person A,person B){
if(A.height!=B.height){
return A.height>B.height;
}else{
return A.name<B.name;
}
}
int main(){
int N,K,rowNumber,remainder,row,index;
vector<person> team, singleRow;
person single;
cin>>N>>K;
rowNumber=N/K;
remainder=N%K;
for(int i=0;i<N;i++){
cin>>single.name>>single.height;
team.push_back(single);
}
sort(team.begin(),team.end(),cmp);
row=rowNumber+remainder;
index=0;
while(index<N){
if(row%2==0){
for(int i=row;i>0;i-=2){
singleRow.push_back(team[index+i-1]);
}
for(int i=1;i<row;i+=2){
singleRow.push_back(team[index+i-1]);
}
}else{
for(int i=row-1;i>0;i-=2){
singleRow.push_back(team[index+i-1]);
}
for(int i=1;i<=row;i+=2){
singleRow.push_back(team[index+i-1]);
}
}
for(int j=0;j<row;j++){
if(j==0){
cout<<singleRow[index+j].name;
}else{
cout<<" "<<singleRow[index+j].name;
}
}
cout<<endl;
index+=row;
row=rowNumber;
}
return 0;
}