PAT A1137 Final Grading (25 分)——排序

For a student taking the online course "Data Structures" on China University MOOC (http://www.icourse163.org/), to be qualified for a certificate, he/she must first obtain no less than 200 points from the online programming assignments, and then receive a final grade no less than 60 out of 100. The final grade is calculated by G=(Gmidterm​​×40%+Gfinal​​×60%) if Gmidterm​​>Gfinal​​, or Gfinal​​ will be taken as the final grade G. Here Gmidterm​​ and Gfinal​​ are the student's scores of the mid-term and the final exams, respectively.

The problem is that different exams have different grading sheets. Your job is to write a program to merge all the grading sheets into one.

Input Specification:

Each input file contains one test case. For each case, the first line gives three positive integers: P , the number of students having done the online programming assignments; M, the number of students on the mid-term list; and N, the number of students on the final exam list. All the numbers are no more than 10,000.

Then three blocks follow. The first block contains P online programming scores Gp​​'s; the second one contains M mid-term scores Gmidterm​​'s; and the last one contains N final exam scores Gfinal​​'s. Each score occupies a line with the format: StudentID Score, where StudentID is a string of no more than 20 English letters and digits, and Score is a nonnegative integer (the maximum score of the online programming is 900, and that of the mid-term and final exams is 100).

Output Specification:

For each case, print the list of students who are qualified for certificates. Each student occupies a line with the format:

StudentID Gp​​ Gmidterm​​ Gfinal​​ G

If some score does not exist, output "1" instead. The output must be sorted in descending order of their final grades (G must be rounded up to an integer). If there is a tie, output in ascending order of their StudentID's. It is guaranteed that the StudentID's are all distinct, and there is at least one qullified student.

Sample Input:

6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

Sample Output:

missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84
 
 1 #include <stdio.h>
 2 #include <string>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <map>
 7 #include <string.h>
 8 using namespace std;
 9 const int maxn=30010;
10 struct stu{
11     int gp=-1;
12     int gm=-1;
13     int gf=-1;
14     int g=0;
15     string id;
16     bool qua=true;
17 }students[maxn];
18 int p,m,n,score;
19 string id;
20 int num=0;
21 map<string,int> s2n;
22 map<int,string> n2s;
23 int string2num(string s){
24     if(s2n.find(s)==s2n.end()){
25         s2n[s]=num;
26         return num++;
27     }
28     else{
29         return s2n[s];
30     }
31 }
32 bool cmp(stu s1,stu s2){
33     return s1.g==s2.g?s1.id<s2.id:s1.g>s2.g;
34 }
35 int main(){
36       scanf("%d %d %d",&p,&m,&n);
37       for(int i=0;i<p;i++){
38           cin>>id>>score;
39           if(score>900) continue;
40           int index=string2num(id);
41           if(n2s.find(index)==n2s.end()){
42               n2s[index]=id;
43               students[index].id=id;
44         }
45           students[index].gp=score;
46     }
47     for(int i=0;i<m;i++){
48           cin>>id>>score;
49           if(score>100) continue;
50           int index=string2num(id);
51           if(n2s.find(index)==n2s.end()){
52               n2s[index]=id;
53               students[index].id=id;
54         }
55           students[index].gm=score;
56     }
57     for(int i=0;i<n;i++){
58           cin>>id>>score;
59           if(score>100) continue;
60           int index=string2num(id);
61           if(n2s.find(index)==n2s.end()){
62               n2s[index]=id;
63               students[index].id=id;
64         }
65           students[index].gf=score;
66     }
67     for(int i=0;i<num;i++){
68         if(students[i].gp<200) students[i].g=-1;
69         else {
70             if(students[i].gm>students[i].gf){
71                 float tt=0.4*students[i].gm+0.6*students[i].gf;
72                 if((tt-(int)tt)>=0.5) students[i].g=(int)tt + 1;
73                 else students[i].g=(int)tt;
74             }
75             else students[i].g = students[i].gf;
76         }
77         if(students[i].g<60) students[i].g=-1;
78     }
79     sort(students,students+num,cmp);
80     for(int i=0;i<num;i++){
81         if(students[i].g==-1) break;
82         else {
83             printf("%s %d %d %d %d
",students[i].id.c_str(),students[i].gp,students[i].gm,students[i].gf,students[i].g);
84         }
85     }
86 }
View Code

注意点:简单的排序题,但还是错了很多遍,第一次是maxn设太小了,题目是说每个不超过10000,所以三个加起来应该是不超过30000,导致最后一个测试点段错误。

第二个坑是算最后得分时要考虑小数的进位,不能直接取整

第三个坑题目里说了不超过900和100,以为没用,不会给错误分数,不加判断会出现超时,但提交第二次就没超时,也不知道为什么

ps:结构体和全局变量好多都是没用的其实,不过先加着也没事,最后出问题了再删吧

---------------- 坚持每天学习一点点
原文地址:https://www.cnblogs.com/tccbj/p/10429438.html