1012 The Best Rank (25 分)

本文介绍了一个学生成绩排名系统的实现方法,该系统使用C++编写,能够根据学生的数学、英语和平均成绩进行排序,并给出相应的排名。文章详细展示了如何利用map和vector进行数据存储与排序,以及如何处理同分情况下的排名问题。

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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480

注意点:map的value值排序,可以通过vector排序解决

参考博客:https://blog.youkuaiyun.com/weixin_41168353/article/details/81203181

AC代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
using namespace std;

//本题难点:对于每个同学的成绩排序
const int maxn = 2010;
struct stu {
	string num;
	int c;
	int m;
	int e;
	int a;
}stu[maxn];


bool cmp(const pair<string, int>&p1, const pair<string, int>&p2) {
	return p1.second > p2.second;
}
bool cmp1(const pair<string, double>&p1, const pair<string, double>&p2) {
	return p1.second > p2.second;
}

int main(){
	int n, m;
	cin >> n >> m;
	set<string> stu_num;//存储学生的姓名,判断输入姓名是否合法
	vector<pair<string, int> > vec1,vec2,vec3;
	vector<pair<string, double> > vec;
	for (int i = 0; i < n; i++) {
		cin >> stu[i].num >> stu[i].c >> stu[i].m >> stu[i].e;
		stu[i].a = (stu[i].c + stu[i].m + stu[i].e) / 3.0 + 0.5;
		vec1.push_back(make_pair(stu[i].num, stu[i].c));
		vec2.push_back(make_pair(stu[i].num, stu[i].m));
		vec3.push_back(make_pair(stu[i].num, stu[i].e));
		vec.push_back(make_pair(stu[i].num, stu[i].a));
		stu_num.insert(stu[i].num);
	}
	sort(vec1.begin(), vec1.end(), cmp);
	//存储学生在c的排名

	map<string, int> rank1;
	rank1[vec1[0].first] = 1;
	for (int i = 1; i < vec1.size(); i++) {
		if (vec1[i].second != vec1[i - 1].second)
			rank1[vec1[i].first] = i + 1;
		else
			rank1[vec1[i].first] = rank1[vec1[i-1].first];
	}
	
	sort(vec2.begin(), vec2.end(), cmp);
	//存储学生在m的排名
	map<string, int> rank2;
	rank2[vec2[0].first] = 1;
	for (int i = 1; i < vec2.size(); i++) {
		if (vec2[i].second != vec2[i - 1].second)
			rank2[vec2[i].first] = i + 1;
		else
			rank2[vec2[i].first] = rank2[vec2[i - 1].first];
	}


	sort(vec3.begin(), vec3.end(), cmp);
	//存储学生在e的排名

	map<string, int> rank3;
	rank3[vec3[0].first] = 1;
	for (int i = 1; i < vec3.size(); i++) {
		if (vec3[i].second != vec3[i - 1].second)
			rank3[vec3[i].first] = i + 1;
		else
			rank3[vec3[i].first] = rank3[vec3[i - 1].first];
	}


	sort(vec.begin(), vec.end(), cmp1);
	//存储学生在a的排名

	map<string, int> rank;
	rank[vec[0].first] = 1;
	for (int i = 1; i < vec.size(); i++) {
		if (vec[i].second != vec[i - 1].second)
			rank[vec[i].first] = i + 1;
		else
			rank[vec[i].first] = rank[vec[i - 1].first];
	}


	for (int i = 0; i < m; i++) {
		string str;
		cin >> str;
		if (stu_num.find(str) == stu_num.end()) {
			printf("N/A\n");
			continue;
		}
		int a = rank1[str], b = rank2[str], c = rank3[str],A=rank[str];
		int max_ = min(A,min(a, min(b, c)));
		if (max_ == A) {
			printf("%d A\n",max_);
		}
		else if (max_ == a) {
			printf("%d C\n", max_);
		}
		else if (max_ == b) {
			printf("%d M\n", max_);
		}
		else if (max_ == c) {
			printf("%d E\n", max_);
		}
	}

}

 

下面这个代码报了,应该怎么改: %%Matlab Genetic Algorithm for Sin Prediction clear; clc; %population size Npop=50; %create the population Pop=rand(Npop,1)*2*pi; %define fitness fit=@(x) sin(x); %fitness score score=fit(Pop); %maximum number of generations maxgen=100; %weights w=0.7; %probability p_crossover=0.9; p_mutation=0.2; %loop for number of generations for gen=1:maxgen %ranking %rank the population in descending order [~,rank]=sort(score); %rank the population in ascending order rank=flipud(rank); %normalised rank NormalisedRank=rank/sum(rank); %selection %cumulative sum of the normalised rank cumulativeSum=cumsum(NormalisedRank); %randomly select the two parents %from the populations based on their %normalised rank randnum=rand; parent1=find(cumulativeSum>randnum,1); randnum=rand; parent2=find(cumulativeSum>randnum,1); %crossover %randomly select the crossover point pc=randi([1 Npop-1]); %create the offsprings offspring1=[Pop(parent1,1:pc) Pop(parent2,pc+1:end)]; offspring2=[Pop(parent2,1:pc) Pop(parent1,pc+1:end)]; %perform crossover with a probability if(rand<p_crossover) Pop=[Pop; offspring1; offspring2]; end %mutation %randomly select the point of mutation pm=randi([1 Npop]); %mutate the value under the chosen point Pop(pm)=rand*2*pi; %perform mutation with a probability if (rand<p_mutation) Pop(pm)=rand*2*pi; end %evaluate new population score=fit(Pop); %elitism %sort the population in ascending order %of their fitness score [score,rank]=sort(score); elite=Pop(rank(1),:); Pop(rank(Npop),:)=elite; %replace old population Pop=Pop(1:Npop,:); end %print the best solution disp(&#39;Best Solution: &#39;); disp(elite);
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值