-
Notifications
You must be signed in to change notification settings - Fork 1
/
1055.c
104 lines (103 loc) · 2.11 KB
/
1055.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
struct student{
char name[10];
int height;
};
int cmp(const void *p,const void *q){
const struct student a=*(const struct student *)p;
const struct student b=*(const struct student *)q;
if(a.height>b.height){ //降序
return -1;
}
else if(a.height<b.height){
return 1;
}
else{
if(strcmp(a.name, b.name)>0){ //升序
return 1;
}
else if(strcmp(a.name, b.name)<0){
return -1;
}
else{
return 0;
}
}
}
void birth(int *p,int len){
int i,temp;
if(len==1){
p[0]=1;
}
else if(len%2==0){
p[0]=1;
for(i=1;i<len/2;i++){
p[i]=p[i-1]+2;
}
if(i<len)
p[i]=p[i-1]+1;
temp=++i;
for(i=temp;i<len;i++){
p[i]=p[i-1]-2;
}
}
else{
p[0]=2;
for(i=1;i<len/2;i++){
p[i]=p[i-1]+2;
}
if(i<len)
p[i]=p[i-1]+1;
temp=++i;
for(i=temp;i<len;i++){
p[i]=p[i-1]-2;
}
}
}
int main(){
int n,k,i,j,r1[10000]={0},r2[10000]={0},flag=0,len1,len2;
struct student arr[10010];
scanf("%d%d",&n,&k);
for(i=0;i<n;i++){
scanf("%s%d",arr[i].name,&arr[i].height);
}
qsort(arr, n, sizeof(struct student), cmp);
if(k>n){
len1=len2=1;
}
else{
len1=n/k+n%k;
len2=n/k;
}
birth(r1, len1);
birth(r2, len2);
for(i=0;i<len1;i++){
if(flag==0){
printf("%s",arr[len1-r1[i]].name);
flag=1;
}
else{
printf(" %s",arr[len1-r1[i]].name);
}
}
flag=0;
for(i=len1;i<n;i=i+len2){
printf("\n");
for(j=0;j<len2;j++){
if(flag==0){
printf("%s",arr[len2-r2[j]+i].name);
flag=1;
}
else{
printf(" %s",arr[len2-r2[j]+i].name);
}
}
flag=0;
}
printf("\n");
return 0;
}