1012 The Best Rank (25)

本文介绍了一个用于管理学生成绩的系统,包括排序、查找排名和显示最佳成绩等功能。

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

参考:http://blog.youkuaiyun.com/lv_zj/article/details/15809007

 

 

#include<iostream>
#include<string.h>
using namespace std;

class CA
{
public:
 enum{MAX=2000};
 void run();
 void findbest(char ID[7]);
 void findrank(int a[4]);
 void sort();
private:
 int n,m;
 int rank;

 struct member
 {
  char StuID[7];
  int CO;
  int MA;
  int EN;
  int AV;
  int ra[4];
 };
 member stu[MAX];
 char subject;
 struct aa
 {
  char aaa[7];
 };
 aa bb[MAX];
};

void CA::sort()
{
 int i,j;

 int tmp=-1;

 for(i=0;i<n;i++)
 {
  stu[i].ra[0]=1;
  for(j=0;j<n;j++)
  {
   if(stu[j].CO>stu[i].CO)
    stu[i].ra[0]++;
  }
 }
 for(i=0;i<n;i++)
 {
  stu[i].ra[1]=1;
  for(j=0;j<n;j++)
  {
   if(stu[j].MA>stu[i].MA)
    stu[i].ra[1]++;
  }
 }
 for(i=0;i<n;i++)
 {
  stu[i].ra[2]=1;
  for(j=0;j<n;j++)
  {
   if(stu[j].EN>stu[i].EN)
    stu[i].ra[2]++;
  }
 }
 for(i=0;i<n;i++)
 {
  stu[i].ra[3]=1;
  for(j=0;j<n;j++)
  {
   if(stu[j].AV>stu[i].AV)
    stu[i].ra[3]++;
  }
 }
 /*for(i=1;i<n;i++)
 {
  for(j=i-1;j>=0;j--)
  {
   if(stu[j].MA<stu[j+1].MA)
   {
    tmp=stu[j].ra[1];
    stu[j].ra[1]=stu[j+1].ra[1];
    stu[j+1].ra[1]=tmp;
   }
   if(stu[j].MA==stu[j+1].MA)
   {
    stu[j+1].ra[1]=stu[j].ra[1];
   }
  }
 }
 for(i=1;i<n;i++)
 {
  for(j=i-1;j>=0;j--)
  {
   if(stu[j].EN<stu[j+1].EN)
   {
    tmp=stu[j].ra[2];
    stu[j].ra[2]=stu[j+1].ra[2];
    stu[j+1].ra[2]=tmp;
   }
   if(stu[j].EN==stu[j+1].EN)
   {
    stu[j+1].ra[2]=stu[j].ra[2];
   }
  }
 }
 for(i=1;i<n;i++)
 {
  for(j=i-1;j>=0;j--)
  {
   if(stu[j].AV<stu[j+1].AV)
   {
    tmp=stu[j].ra[3];
    stu[j].ra[3]=stu[j+1].ra[3];
    stu[j+1].ra[3]=tmp;
   }
   if(stu[j].AV==stu[j+1].AV)
   {
    stu[j+1].ra[3]=stu[j].ra[3];
   }
  }
 }*/
}

void CA::findrank(int a[4])
{
 rank=a[3];
 subject='A';
 if(a[0]<rank)
 {
  rank=a[0];
  subject='C';
 }
 if(a[1]<rank)
 {
  rank=a[1];
  subject='M';
 }
 if(a[2]<rank)
 {
  rank=a[2];
  subject='E';
 }
}

void CA::findbest(char ID[7])
{
 rank=-1;
 int i;
 
 for(i=0;i<n;i++)
 {
  if(strcmp(stu[i].StuID,ID)==0)
  {
   findrank(stu[i].ra);
   switch(subject)
   {
   case 'A':printf("%d ",stu[i].ra[3]);break;
   case 'C':printf("%d ",stu[i].ra[0]);break;
   case 'M':printf("%d ",stu[i].ra[1]);break;
   case 'E':printf("%d ",stu[i].ra[2]);break;
   }
   printf("%c\n",subject);
   break;
  }
 }
 if(rank==-1)
  printf("N/A\n");
}

void CA::run()
{
 cin>>n>>m;
 int i;
 for(i=0;i<n;i++)
 {
  cin>>stu[i].StuID>>stu[i].CO>>stu[i].MA>>stu[i].EN;
  stu[i].AV=(stu[i].CO+stu[i].MA+stu[i].EN)/3;
 }
 
 sort();

 for(i=0;i<m;i++)
 {
  cin>>bb[i].aaa; 
 }
 for(i=0;i<m;i++)
 {
  findbest(bb[i].aaa); 
 }
 
}

int main()
{
 CA *a=new CA;
 a->run();
 return 0;
}

下面这个代码报错了,应该怎么改: %%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、付费专栏及课程。

余额充值