原题链接:1137 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=(Gmid−term×40%+Gfinal×60%) if Gmid−term>Gfinal, or Gfinal will be taken as the final grade G. Here Gmid−term 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 Gmid−term’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 Gmid−term 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
代码:
#include <iostream>
#include <unordered_map>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 10000 + 10;
struct Student{
string id;
int g_p = -1, g_m = -1, g_f = -1, grade = 0; //编程成绩,期中成绩,期末成绩,最终成绩
};
unordered_map<string, Student> map;
vector<Student> res;
void check(Student &a){ //确定最终成绩
if(a.g_m >= a.g_f) a.grade = round(a.g_m * 0.4 + a.g_f * 0.6 );
else a.grade = a.g_f;
}
bool cmp(Student a,Student b){
if(a.grade != b.grade)
return a.grade > b.grade;
return a.id < b.id;
}
int main(){
int p, m, n;
scanf("%d%d%d", &p, &m, &n);
for(int i = 0; i < p; i ++ ){
string s;
int a;
cin >> s >> a;
map[s].id = s;
map[s].g_p = a;
}
for(int i = 0; i < m; i ++ ){
string s;
int a;
cin >> s >> a;
map[s].id = s;
map[s].g_m = a;
}
for(int i = 0; i < n; i ++ ){
string s;
int a;
cin >> s >> a;
//scanf("%s%d", &s, &a); 用scanf过不了,会Segmentation Fault
map[s].id = s;
map[s].g_f = a;
}
for(auto i:map){
if(i.second.g_p < 200) continue;
check(i.second);
if(i.second.grade >= 60)
res.push_back(i.second);
}
sort(res.begin(), res.end(), cmp );
for(auto i:res)
printf("%s %d %d %d %d\n", i.id.c_str(), i.g_p, i.g_m, i.g_f, i.grade); //会乱码需要加上.c_str()
//cout<<i.id<<' '<<i.g_p<<' '<<i.g_m<<' '<<i.g_f<<' '<<i.grade<<endl;
return 0;
}
遇到的问题:
- 使用
printf
输出结果会乱码,使用cout
能正常输出。
因为printf
里面要求是char*类型,string
和char*
是不一样的,需要加上c_str()
:printf("%s", str.c_str());
- 使用
cin
读入正常,scanf
读入时会Segmentation Fault
错误
STL的string不能用scanf读入