中国大学MOOC-数据结构基础习题集、08-1、Talent and Virtue

题目链接:http://www.patest.cn/contests/mooc-ds/08-1

题目分析:这是一道考察排序算法的一道题。如果对题目不懂的话,这里恰巧有一个中文题目,不妨看一下:

  http://www.patest.cn/contests/pat-b-practise/1015

特别说明:

  1. 这道题有一个很大的坑,如果用C++的cin和cout运行会超时。要使用C语言的scanf和printf。博主也是看了别人的博客才发现问题所在。如果你的用例3用例4超时的话,不妨更改一下。

  2. 建立4个vector,分别存圣人、君子、愚人、小人的信息。对每个vector的排序算法相同:先总分降序、再德分降序、再准考证号升序。因此,排序函数是本道题的重点,解决了排序问题,也就解决了这道题。博主这里使用的是标准库中是sort函数,使用方法非常简单:

1     sort(vec1.begin(), vec1.end(), cmp);
2     // 以下省略其他3个vector

  关键是如何编写cmp函数,这里我们按照题目的需求,编写cmp函数如下:

 1 int cmp(const node &x, const node &y)
 2 {
 3     int scoreA = x.b + x.c;
 4     int scoreB = y.b + y.c;
 5     if (scoreA != scoreB)
 6         return scoreA > scoreB;
 7     else
 8     {
 9         if (x.b != y.b)
10             return x.b > y.b;
11         else
12             return x.a < y.a;
13     }
14 }

  3. 其实博主这里偷懒了,没有自己写排序函数。事实上,标准库的排序函数(sort,qsort,stable_sort)在大多数情况都能满足我们的需求。如果能借助现有的函数(或工具)能简化我们的编码,既能提高我们的编码效率,又能让别人更容易读懂我们的代码,出错率低、可移植性高,那么何乐而不为呢?当然,对于“数据结构”这门课程来言,其实还是推荐大家自己编写代码的。

代码分析:

  • 头文件声明及结构体的定义:1~16
  • cmp函数的定义(核心):17~30
  • 处理输入部分,并判断是圣人、君子、愚人或小人,放入对应vector里:31~55
  • 调用排序函数,输出最后结果:56~70
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 struct node
 8 {
 9     int a, b, c;
10     node(int x, int y, int z):a(x), b(y), c(z) {}
11     void output()
12     {
13         printf("%d %d %d\n", a, b, c);
14     }
15 };
16 
17 int cmp(const node &x, const node &y)
18 {
19     int scoreA = x.b + x.c;
20     int scoreB = y.b + y.c;
21     if (scoreA != scoreB)
22         return scoreA > scoreB;
23     else
24     {
25         if (x.b != y.b)
26             return x.b > y.b;
27         else
28             return x.a < y.a;
29     }
30 }
31 
32 int main()
33 {
34     int n, l, h;
35     scanf("%d%d%d", &n, &l, &h);
36     vector<node> vec1;
37     vector<node> vec2;
38     vector<node> vec3;
39     vector<node> vec4;
40     for(int i=0; i<n; i++)
41     {
42         int a, b, c;
43         scanf("%d%d%d", &a, &b, &c);
44         if(b < l || c < l)
45             continue;
46         if(b >= h && c >= h)
47             vec1.push_back(node(a, b, c));  // 圣人
48         else if(b >= h && c < h)
49             vec2.push_back(node(a, b, c));  // 君子
50         else if(b < h && c < h && b >= c)
51             vec3.push_back(node(a, b, c));  // 愚人
52         else
53             vec4.push_back(node(a, b, c));  // 小人
54 
55     }
56     printf("%d\n", vec1.size() + vec2.size() + vec3.size() + vec4.size());
57     sort(vec1.begin(), vec1.end(), cmp);
58     sort(vec2.begin(), vec2.end(), cmp);
59     sort(vec3.begin(), vec3.end(), cmp);
60     sort(vec4.begin(), vec4.end(), cmp);
61     for(int i=0; i<vec1.size(); i++)
62         vec1[i].output();
63     for(int i=0; i<vec2.size(); i++)
64         vec2[i].output();
65     for(int i=0; i<vec3.size(); i++)
66         vec3[i].output();
67     for(int i=0; i<vec4.size(); i++)
68         vec4[i].output();
69     return 0;
70 }

AC成果:

 

posted @ 2015-01-22 23:19  聪明的聪聪  阅读(496)  评论(3编辑  收藏  举报