PAT 1109

题目 : Group Photo

分值 : 25
难度 : 中等题
思路 : 按照题目要求自定义 排序规则: 1)首先根据身高和名字字典序排列 2)然后根据他的排序规则自定义顺序
       一般排序是  6 4 2 1 3 5 7 :因此 1)偶数 > 奇数 2)同为偶数 则降序 3) 同为奇数则升序
坑点 : sort什么时候 降序什么时候 升序了解一下  直接看 符号   a> b  起飞法则 就是降序

具体代码如下

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
#include <iostream>
#include <algorithm>
using namespace std ;
typedef struct Node
{
string name ;
int height;
int group_rank;
}Nodes ;
bool cmp (Node a ,Node b)
{
if(a.height == b.height)
return a.name < b.name ;
return a.height > b.height ;
}
bool cmp2(Node a , Node b )
{
if(a.group_rank%2==b.group_rank%2) //同奇偶性
{
if(a.group_rank%2 == 0 )
return a.group_rank> b.group_rank;
else return a.group_rank< b.group_rank ;
}
return a.group_rank%2 < b.group_rank%2 ;
}

Nodes Q[10001];
int main() {
int N , K ;
cin >> N >>K ;
for(int i = 0 ; i< N ; i++)
{
cin >> Q[i].name >> Q[i].height ;
}
sort(Q,Q+N,cmp);
int m = N/K ,temp ,cur = 0 ;
for(int i = 0 ; i< K ;i++)
{
int start = cur;
temp = m+1;
if(!i) temp = m +1+ N%K ;
for(int j = 1 ; j < temp ;j++)
{
Q[cur++].group_rank = j ;
}
sort(Q+start,Q+cur ,cmp2 ) ;
for(int j = start ; j <cur ; j++)
{
if(j!=start)
cout << " ";
cout << Q[j].name;
}
cout <<endl ;
}

}