1012 The Best Rank (25 分)

本文详细解析了一种用于处理学生多科成绩排名的算法。该算法不仅考虑了单科成绩,还综合考量了平均成绩,确保了排名的公平性和准确性。通过实际案例,展示了如何在C++中实现这一算法,包括数据输入、处理和输出的全过程。

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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805502658068480
题目大意:给你n个同学的成绩信息以及m个同学的询问,对于每个询问,请你给出他最好的排名结果,并输出依据的科目。成绩信息包括学生ID、C语言、Math、English。科目排名优先级为:A > C > M > E (A为平均成绩),即如果某人C和A都是一百分,则输出1 A ,而不是1 C 。对于不存在的学生,输出N/A。

注意:5个同学的成绩如果为90 90 90 89 87 ,则第四位同学的排名为4 而不是2

#include<bits/stdc++.h>
using namespace std;
const int maxn=2000+10;
int n,mm,x,y,z;
double w;
string str;
char ans;
map<string,int> mp;
int C[maxn],M[maxn],E[maxn];
double A[maxn];
struct node{
	int c,m,e;
	double a;
}stu[maxn];
int FindA(int p){
	int r=0;
	double pre,cur;
	for(int i=0;i<n;i++){
		if(i==0) pre=cur=A[i],r=1; 
		else {
			cur=A[i];
			if(cur!=pre) {
				r=i+1;
				pre=cur;
			} 
		}
		if(A[i]==stu[p].a) break;
	}
	return r;
}
int FindC(int p){
	int r=0,pre,cur;
	for(int i=0;i<n;i++){
		if(i==0) pre=cur=C[i],r=1; 
		else {
			cur=C[i];
			if(cur!=pre) {
				r=i+1;
				pre=cur;
			} 
		}
		if(C[i]==stu[p].c) break;
	}
	return r;
}
int FindM(int p){
	int r=0,pre,cur;
	for(int i=0;i<n;i++){
		if(i==0) pre=cur=M[i],r=1; 
		else {
			cur=M[i];
			if(cur!=pre){
				r=i+1;
				pre=cur;
			} 
		}
		if(M[i]==stu[p].m) break;
	}
	return r;
}
int FindE(int p){
	int r=0,pre,cur,cnt=0;
	for(int i=0;i<n;i++){
		if(i==0) pre=cur=E[i],r=1; 
		else {
			cur=E[i];
			if(cur!=pre) {
				r=i+1;
				pre=cur;
			} 
		}
		if(E[i]==stu[p].e) break;
	}
	return r;
}
int main()
{
	cin>>n>>mm;
	for(int i=0;i<n;i++){
		cin>>str;
		mp[str]=i+1;
		cin>>x>>y>>z;
		w=(x+y+z)*1.0/3;
		stu[i+1].a=w,stu[i+1].c=x,stu[i+1].m=y,stu[i+1].e=z;
		A[i]=w;
		C[i]=x;
		M[i]=y;
		E[i]=z;
	}
	sort(A,A+n,greater<double>());
	sort(C,C+n,greater<int>());
	sort(M,M+n,greater<int>());
	sort(E,E+n,greater<int>());
	
	for(int i=1;i<=mm;i++){
		cin>>str;
		if(!mp[str]) cout<<"N/A"<<endl;
		else{
			int rank=0x3f3f3f3f;
			x=mp[str];
			int tmp=FindE(x);
			if(tmp<=rank){
				rank=tmp,ans='E';
			}
			tmp=FindM(x);
			if(tmp<=rank){
				rank=tmp,ans='M';
			}
			tmp=FindC(x);
			if(tmp<=rank){
				rank=tmp,ans='C';
			}
			tmp=FindA(x);
			if(tmp<=rank){
				rank=tmp,ans='A';
			}
			printf("%d %c\n",rank,ans);
		}
	}
 } 
下面这个代码报错了,应该怎么改: %%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('Best Solution: '); disp(elite);
02-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值