-
题目见这里
-
实际上是结构体的排序,然后按特定规则重新填充
-
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10005
typedef struct{
int height;
char name[9];
}People;
People people[N],tmp[N];
int n,k;
void Read(){
int i;
scanf("%d%d",&n,&k);
getchar();
for(i=0;i<n;i++){
scanf("%s%d",people[i].name,&people[i].height);
getchar();
}
}
void Copy(People *to, People *from){
int i;
for(i=0;i<n;i++){
// to[i].height = from[i].height;
// strcpy(to[i].name,from[i].name);
to[i] = from[i];
}
}
//1.身高从高到底
//2.身高相同,按姓名字母表顺序排(小在前)
int Cmp(const void *a, const void *b){
People pa = *(People *)a;
People pb = *(People *)b;
if(pa.height!=pb.height) return pb.height-pa.height;
else return strcmp(pa.name,pb.name);
}
void Solve(){
int s = 0;
int j = -1;
int i,rowP,step,front,rear;
for(i=0;i<k;i++){
if(i==0) rowP = n/k+n%k;
else rowP = n/k;
people[rowP/2+s] = tmp[++j];
rear = front = rowP/2+s;
step = 1;
while(step<rowP){
if(step%2) people[--front] = tmp[++j];
else people[++rear] = tmp[++j];
step ++;
}
s += rowP;
}
}
void Show(){
int s = 0;
int i,j,rowP;
for(i=0;i<k;i++){
if(!i) rowP = n/k+n%k;
else rowP = n/k;
for(j=s;j<s+rowP;j++){
printf("%s",people[j].name);
if(j<s+rowP-1) printf(" ");
else printf("\n");
}
s += rowP;
}
}
int main(){
// freopen("Data.txt","r",stdin);
Read();
Copy(tmp,people);
qsort(tmp,n,sizeof(tmp[0]),Cmp);
Solve();
Show();
return 0;
}