1137 Final Grading——结构体set的比较函数+隐式散列思想+四舍五入小技巧

本文介绍了一个学生成绩管理系统的设计与实现,该系统使用C++语言,通过输入学生的在线、期中和期末成绩,计算总评并进行排序,最后输出成绩大于等于60分的学生名单及其详细成绩。系统采用结构体和自定义比较函数对数据进行处理和排序,确保输出结果的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

大致思路是:输入+处理1;输出+处理2

粗处理:注意找到数组最大的边界
赋值-1给期中成绩(在线和期末是不需要,原因是在输入时已经筛选了)

处理1:输入时;根据输入给的顺序(在线,期中)进行筛选
在输入期末时,此时同时计算总评,并插入set进行排序

处理2:判断是否总评大于60

输出按照期末的递减顺序;若相同,则按照学生id升序排列

其他

四舍五入:通过对结果+0.5,以保证截断时为四舍五入

cmp比较函数必须写到struct中去,同时内部用operation、const
注:set和map都是只能比较 键(给定结构中的成员变量),不能比较 值 或者 调用外部的函数来获取遍历

代码

#include <bits/stdc++.h>
#include <unordered_map>
#pragma warning(disable:4996)
#pragma warning(disable:4700)
#pragma warning(disable:4703)
using namespace std;

struct node {
	string name;
	int score;
	node(string x, int a, int b) {
		name = x;
		a > b ? score = (int)(a * 0.4 + b * 0.6 + 0.5) : score = b;
	}
};
struct cmp {
	bool operator()(node x, node y)const {
		if (x.score > y.score)
			return true;
		if (x.score == y.score) {
			if (x.name < y.name)
				return true;
		}
		return false;
	}
};
int online_n, mid_n, final_n, max_size;
set<node,cmp>print;
vector<int>online_score, mid_score, final_score;
map<string, int>name_id;

void InPut();
int main() {
	InPut();
	int id;
	for (auto i : print) {
		if (i.score >= 60) {
			cout << i.name;
			id = name_id[i.name];
			printf(" %d %d %d %d\n", online_score[id], mid_score[id], final_score[id], i.score);
		}
	}
	return 0;
}
void InPut() {
	string x;
	int y, max_size, id{ 0 };
	map<string, int>::iterator it;
	scanf("%d %d %d", &online_n, &mid_n, &final_n);
	max_size = max(online_n, mid_n);
	max_size = max(max_size, final_n);
	online_score.resize(max_size);
	mid_score.resize(max_size);
	fill(mid_score.begin(), mid_score.end(), -1);
	final_score.resize(max_size);

	for (int i = 0; i < online_n; ++i) {
		cin >> x;
		scanf("%d", &y);
		if (y >= 200) {
			online_score[id] = y;
			name_id[x] = id;
			id++;
		}
	}
	for (int i = 0; i < mid_n; ++i) {
		cin >> x;
		scanf("%d", &y);
		it = name_id.find(x);
		if (it != name_id.end()) {
			mid_score[it->second] = y;
		}
	}
	for (int i = 0; i < final_n; ++i) {
		cin >> x;
		scanf("%d", &y);
		it = name_id.find(x);
		if (it != name_id.end()) {
			final_score[it->second] = y;
			print.insert({ x, mid_score[it->second], final_score[it->second] });
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值