您的位置:首页 > 其它

PAT乙级 1055. 集体照 (25)

2017-09-23 17:08 337 查看
//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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: