PAT乙级1080: MOOC期终成绩

题目:

解题思想:

    题目上有一句“必须首先获得不少于200分的在线编程作业分”,这是解题的关键,所以我们首先要排除编程分少于200的(只有编程成绩不少于200的人的信息才要记录)。因为一开始只能读入编程成绩,所以还没读入的成绩保存为-1(也正好符合题目上说的)。顺便把(学号-下标)保存到map容器中(为了添加其他成绩的时候能直接判断该同学的编程成绩是否合格和直接通过下标来更新他的其它成绩)。最后计算出最终成绩,把合格的同学筛选打印出来。 

题解:

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
class Student
{
public:
	string no;
	int pscore, mscore,fscore,sumscore;
	Student(string no, int pscore, int mscore, int fscore, int sumscore)
	{
		this->no = no;
		this->pscore = pscore;
		this->mscore = mscore;
		this->fscore = fscore;
		this->sumscore = sumscore;
	}
};
bool cmp(Student v1, Student v2)
{
	if (v1.sumscore != v2.sumscore)
		return v1.sumscore>v2.sumscore;
	else
		return v1.no<v2.no;
}
int main()
{
//	freopen("D://test.txt","r",stdin);
	string no;
	int score, p, m, f, cnt = 1;
	vector<Student> v;//用来保存对象(学号成绩信息) 
	map<string, int> mp; //用来保存学号和容器下标 
	cin >> p >> m >> f;
	//保存编程成绩和学号 
	for (int i = 0; i<p; i++)
	{
		cin >> no>> score;
		if (score >= 200)
		{
			v.push_back(Student(no, score, -1, -1, 0));
			//保存的下标+1,因为通过[]访问不存在的值返回的就是0,所以下标+1来作为区别 
			mp.insert(make_pair(no, cnt++)); 
		}
	}
	//更新期中成绩,没有更新的依旧为-1 
	for (int i = 0; i<m; i++)
	{
		cin >> no >> score;
		//如果该学号的人在map容器中查找不到,则mp[no]为0 
		if (mp[no] != 0)
		{
			v[mp[no] - 1].mscore = score;
		}
	}
	//更新期末成绩 
	for (int i = 0; i<f; i++)
	{
		cin >> no >> score;
		if (mp[no] != 0)
		{
			v[mp[no] - 1].fscore = score;
		}
	}
	//计算最终成绩 
	for (unsigned int i = 0; i<v.size(); i++)
	{
		int mscore = 0, fscore = 0;
		if (v[i].mscore != -1)
			mscore = v[i].mscore;
		if (v[i].fscore != -1)
			fscore = v[i].fscore;
		if (mscore>fscore)
		{
			//浮点数转换成整数只会向下取整,加上0.5后就能四舍五入 
			v[i].sumscore = mscore*0.4 + fscore*0.6+0.5;
		}
		else
			v[i].sumscore = fscore;
	} 
	sort(v.begin(), v.end(), cmp);
	//打印出最终成绩不小于60的同学 
	for (auto it = v.begin(); it != v.end(); it++)
	{
		if (it->sumscore >= 60)
		{
			cout << it->no << " " << it->pscore << " " << it->mscore << " " << it->fscore << " " << it->sumscore << endl;
		}
	}
	return 0;
}

踩坑:

  • 如果map<string,int> a;容器中原先不存在键值为1的元素对,则a["1"]等于0。
  • 四舍五入的取法,加上0.5再取整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值