1080 MOOC期终成绩 (25 point(s)) (测试点三)

当时翻遍了测试点三的错误可能分析,比如排序问题,-1 或者成绩为 0 问题,或者是四舍五入问题。但是看自己代码似乎都考虑了这些问题。

后面看了了别人的数据类型都是 int 想了想自己的是 double 会不会这个有问题。改成 int 之后果断 AC 。

但不太能够理解为什么用 double 会使得结果有误,前面有些题目也是用 double 可以解决的。你说 -1 但是 19 分的代码里面是分开 -1 对此进行了判断的,不会算 -1 进去。如果是四舍五入,加 0.5 之后跟整形也差不多吧,实际上当乘以 0.4 0.6 计算时就是当成 double 计算的,不明白用 double 存储为什么就会有问题了。有点怪。


输出前要把名字存到结构体里面,而每次不同成绩都会有名字,但只需要最后期末输入的时候存一下即可。

因为 Gp 编程没有成绩直接不合格,只有期中成绩的话 就算是满分 40 分同样不合格,所以要合格的必然期末会有名字,只需要处理这些人录入其名字。


image

吐槽下这段描述,当时看到这个如果没看到后面的分号,还以为这个条件是跟着后面的,但实际这个如果是跟着前面的计算公式。神奇的倒装。


for(auto p: pass)
	cout << stu[name].name << " " << stu[name].Gp << " " << stu[name].Gm << " " << stu[name].Gf << " " << stu[name].G << endl;

最开始的时候因为这个输出太长了,所以就用了复制粘贴。但没注意循环是迭代器 p ,却写错成了容器 stu[name] ,所以当时一直输出同样的数据,但是行数跟结果是一致的,我当时还以为是处理上有问题。但实际是写错容器了。

因为这个错误调试了 15 分钟。


开始的时候想在 map 里面重载 < 运算符来重置它的排序方式,但是发现没有效果。

后来查了发现,我把 set 重载方式放到 map 是不对的, map 要重写一个排序函数在放到 map 里面。不过当时想到也不一定要用 map 所以临时改成 vector 了。以后可以试试 map 的排序重载。

C++ STL中Map的按Key排序和按Value排序

// AC
#include <bits/stdc++.h>
using namespace std;

struct Stu{
	string name;
	int Gp = 0, Gm = -1, Gf = -1, G = 0;
};

int main() {
	int P, M, N, score;
	string name;
	map<string, Stu> stu;
	vector<Stu> pass;
	cin >> P >> M >> N;
	
	while(P--){
		cin >> name >> score;
		stu[name].Gp = score;
	}
	
	while(M--){
		cin >> name >> score;
		stu[name].Gm = score;
	}
	while(N--){
		cin >> name >> score;
		stu[name].Gf = score;
		stu[name].name = name;
		
		stu[name].G = stu[name].Gf;
		// 如果计算总评期中大于期末 
		if(stu[name].Gm > stu[name].Gf) 
			stu[name].G = stu[name].Gm * 0.4 + stu[name].Gf * 0.6 + 0.5;
		
		// 判断是否符合合格条件
		if(stu[name].Gp >= 200 && stu[name].G >= 60) pass.push_back(stu[name]); 
	}
	
	sort(begin(pass), end(pass), [](Stu s1, Stu s2){
		if (s1.G != s2.G)
			return s1.G > s2.G;
		return s1.name < s2.name;
	});
	
	for(auto p: pass)
		cout << p.name << " " << p.Gp << " " << p.Gm << " " << p.Gf << " " << p.G << endl;
	
}
// 19 points
#include <bits/stdc++.h>
using namespace std;

struct Stu{
	string name;
	double Gp = 0, Gm = -1, Gf = -1, G = 0;
};

int main() {
	double P, M, N, score;
	string name;
	map<string, Stu> stu;
	vector<Stu> pass;
	cin >> P >> M >> N;
	
	while(P--){
		cin >> name >> score;
		stu[name].Gp = score;
	}
	while(M--){
		cin >> name >> score;
		stu[name].Gm = score;
	}
	
	while(N--){
		cin >> name >> score;
		stu[name].Gf = score;
		stu[name].name = name;
		
		// 计算总评 如果期中大于期末 
		if(stu[name].Gm < stu[name].Gf) stu[name].G = stu[name].Gf;
		else{
			// 判断成绩是否存在
			if(stu[name].Gm == -1) 
				stu[name].G = stu[name].Gf * 0.6;
			else if(stu[name].Gf == -1)
				stu[name].G = stu[name].Gm * 0.4;
			else
				stu[name].G = stu[name].Gm * 0.4 + stu[name].Gf * 0.6;
			// 四舍五入
			stu[name].G += 0.5;
		} 
		
		// 判断是否符合合格条件
		if(stu[name].Gp >= 200 && stu[name].G >= 60) pass.push_back(stu[name]); 
	}
	sort(begin(pass), end(pass), [](Stu s1, Stu s2){
		if (s1.G != s2.G)
			return s1.G > s2.G;
		return s1.name < s2.name;
	});
	
	for(auto p: pass)
		cout << p.name << " " << p.Gp << " " << p.Gm << " " << p.Gf << " " << (int)p.G << endl;
}

posted on 2021-09-16 00:04  Atl212  阅读(201)  评论(0)    收藏  举报

导航