Article From:https://www.cnblogs.com/moujun1001/p/9970385.html

subject

    Topic address:PAT Class B 1015

 

Title Solution

    routineThe difficulty lies in clarifying the sorting rules and coding them in a concise way.

    Here I choose to use vector for storage and sort by sort method, because this topic is not simply sorted by size, so we need to understand the third parameter of sort method and its use details again.

    About the specific use of sort, you can use it in mePrevious blog postsSee;

    One final point that needs to be clarified is that:Use printf and scanf as much as possible to reduce the use of cout and CIN,In the past, CIN and cout were used, and the result was that the time limit was exceeded…

 

Code

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct Stu {
 7     int num;
 8     int s1;
 9     int s2;
10 };
11 
12 bool comp(Stu x, Stu y) {
13     if (x.s1 + x.s2 > y.s1 + y.s2) return true;
14     else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 > y.s1) return true;
15     else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 == y.s1 && x.num < y.num) return true;
16     else return false;
17 }
18 
19 int main() {
20     vector<Stu> stu1, stu2, stu3, stu4;
21     int tmp_num, tmp_s1, tmp_s2;
22     Stu tmp_stu;
23     int N = 0, L = 0, H = 0;
24     scanf("%d %d %d", &N, &L, &H);
25     while (N--) {
26         scanf("%d %d %d", &tmp_num, &tmp_s1, &tmp_s2);
27         tmp_stu.num = tmp_num;
28         tmp_stu.s1 = tmp_s1;
29         tmp_stu.s2 = tmp_s2;
30         if (tmp_stu.s1 >= L && tmp_stu.s2 >= L) {
31             if (tmp_stu.s1 >= H && tmp_stu.s2 >= H) stu1.push_back(tmp_stu);
32             else if (tmp_stu.s1 >= H && tmp_stu.s2 < H) stu2.push_back(tmp_stu);
33             else if (tmp_stu.s1 < H && tmp_stu.s2 < H && tmp_stu.s1 >= tmp_stu.s2) stu3.push_back(tmp_stu);
34             else stu4.push_back(tmp_stu);
35         }
36     }
37     sort(stu1.begin(), stu1.end(), comp);
38     sort(stu2.begin(), stu2.end(), comp);
39     sort(stu3.begin(), stu3.end(), comp);
40     sort(stu4.begin(), stu4.end(), comp);
41     printf("%d\n", stu1.size() + stu2.size() + stu3.size() + stu4.size());
42     for (int i = 0; i < stu1.size(); i++)
43         printf("%d %d %d\n", stu1[i].num, stu1[i].s1, stu1[i].s2);
44     for (int i = 0; i < stu2.size(); i++)
45         printf("%d %d %d\n", stu2[i].num, stu2[i].s1, stu2[i].s2);
46     for (int i = 0; i < stu3.size(); i++)
47         printf("%d %d %d\n", stu3[i].num, stu3[i].s1, stu3[i].s2);
48     for (int i = 0; i < stu4.size(); i++)
49         printf("%d %d %d\n", stu4[i].num, stu4[i].s1, stu4[i].s2);
50 
51     return 0;
52 }

    There is still some redundancy in the code, especially in the sorting and input parts, so the slightly improved code is pasted on it.

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 struct Stu {
 7     int num;
 8     int s1;
 9     int s2;
10 };
11 
12 bool comp(Stu x, Stu y) {
13     if (x.s1 + x.s2 > y.s1 + y.s2) return true;
14     else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 > y.s1) return true;
15     else if (x.s1 + x.s2 == y.s1 + y.s2 && x.s1 == y.s1 && x.num < y.num) return true;
16     else return false;
17 }
18 
19 int main() {
20     vector<Stu> stu[4];
21     int tmp_num, tmp_s1, tmp_s2;
22     Stu tmp_stu;
23     int N = 0, L = 0, H = 0;
24     scanf("%d %d %d", &N, &L, &H);
25     while (N--) {
26         scanf("%d %d %d", &tmp_num, &tmp_s1, &tmp_s2);
27         tmp_stu.num = tmp_num;
28         tmp_stu.s1 = tmp_s1;
29         tmp_stu.s2 = tmp_s2;
30         if (tmp_stu.s1 >= L && tmp_stu.s2 >= L) {
31             if (tmp_stu.s1 >= H && tmp_stu.s2 >= H) stu[0].push_back(tmp_stu);
32             else if (tmp_stu.s1 >= H && tmp_stu.s2 < H) stu[1].push_back(tmp_stu);
33             else if (tmp_stu.s1 < H && tmp_stu.s2 < H && tmp_stu.s1 >= tmp_stu.s2) stu[2].push_back(tmp_stu);
34             else stu[3].push_back(tmp_stu);
35         }
36     }
37     printf("%d\n", stu[0].size() + stu[1].size() + stu[2].size() + stu[3].size());
38     for (int i = 0; i < 4; i++) {
39         sort(stu[i].begin(), stu[i].end(), comp);
40         for (int j = 0; j < stu[i].size(); j++)
41            printf("%d %d %d\n", stu[i][j].num, stu[i][j].s1, stu[i][j].s2);
42     }
43 
44     return 0;
45 }

    So the whole code is simpler and less bulky.

Link of this Article: PAT Class B 1015

Leave a Reply

Your email address will not be published. Required fields are marked *