1153 Decode Registration Card of PAT

在结构体中,不用定义构造函数,实现快速初始化结构体。

struct Student {
	string ID;
	int score;
} ;
vector<Student> v;
v.push_back({"我好帅",250}); //快速初始化Student

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<unordered_map>
 4 #include<algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 10010;
 8 struct Student {
 9     string ID;
10     int score;
11 } stu[maxn];
12 
13 bool cmp(const Student& a,const Student& b) {
14     return a.score != b.score ? a.score > b.score: a.ID < b.ID;
15 }
16 
17 int main() {
18     int n,m;
19     cin>>n>>m;
20     for(int i = 0; i < n; ++i)
21         cin>>stu[i].ID>>stu[i].score;
22     for(int k = 1; k <= m; ++k) {
23         int type,flag = 0;
24         string term;
25         cin>>type>>term;
26         printf("Case %d: %d %s\n",k,type,term.c_str());
27         if(type == 1) {
28             sort(stu,stu+n,cmp);
29             for(int i = 0; i < n; ++i) {
30                 if(stu[i].ID[0] == term[0]) {
31                     if(flag == 0 ) flag = 1;
32                     printf("%s %d\n",stu[i].ID.c_str(),stu[i].score);
33                 }
34             }
35         } else if(type == 2) {
36             int NT = 0,NS = 0;
37             for(int i = 0; i < n; ++i) {
38                 if(stu[i].ID.substr(1,3) == term) {
39                     if(flag == 0) flag = 1;
40                     NT++;
41                     NS += stu[i].score;
42                 }
43             }
44             if(flag == 1)
45                 printf("%d %d\n",NT,NS);
46         } else if(type == 3) {
47             unordered_map<string,int> mp;
48             for(int i = 0; i < n; ++i) {
49                 if(stu[i].ID.substr(4,6) == term) {
50                     if(flag == 0) flag = 1;
51                     mp[stu[i].ID.substr(1,3)]++;
52                 }
53             }
54             vector<Student> v;
55             for(auto it:mp) v.push_back({it.first,it.second}); //快速初始化Student
56             sort(v.begin(),v.end(),cmp);
57             for(int i = 0; i < v.size(); ++i)
58                 printf("%s %d\n",v[i].ID.c_str(),v[i].score);
59         }
60         if(flag == 0) printf("NA\n");
61     }
62     return 0;
63 }

 

posted @ 2020-03-12 21:48  tangq123  阅读(120)  评论(0)    收藏  举报