1012. The Best Rank (25)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:57   3369   0

通过对输入数据进行处理,从而来获得自己单科或者平均的最好成绩。

一开始,利用vector<char *>想来保存动态变化的字符串数组,但老师不成功,

最后利用vector<string>来完成,并且通过cin>>输入。

代码如下:

#include"stdio.h"
#include"string.h"
#include"math.h"
#include<iostream>
#include<vector>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;
#define INF 0x7FFFFFFF

struct score{
 string stuNum;
 int c;
 int m;
 int e;
 int a;
 int rank;
 char subject;
};

vector<struct score> all;
vector<string> request;

//count the best rank of his in a,c,m,e
void countBestRank(int *final,int c,int m,int e,int a){
 int cm = (c>m)? m:c;
 int cme = (cm>e)? e:cm;
 int cmea = (cme>a)?a:cme;
 if(cmea == a){
  final[0] = 65;
 }else if(cmea == c){
  final[0] = 67;
 }else if(cmea == m){
  final[0] = 77;
 }else if(cmea == e){
  final[0] = 69;
 }
 final[1] = cmea;
}
int main(){
 int students,requestStus;
 scanf("%d %d",&students,&requestStus);
 all.resize(students);
 request.resize(requestStus);
 
 for(int i = 0;i < students;i++){
  cin>>all[i].stuNum;
  scanf("%d %d %d",&all[i].c,&all[i].m,&all[i].e);
  all[i].a = (all[i].c + all[i].m + all[i].e)/3;
 }
 for(int i = 0;i < requestStus;i++){
  cin>>request[i];
 }
 for(int i = 0;i < students;i++){
  int tempRankC = 0,tempRankM = 0,tempRankE = 0,tempRankA = 0;
  for(int j = 0;j < students;j++){
   if(all[i].c < all[j].c){
    tempRankC++;
   }
   if(all[i].m < all[j].m){
    tempRankM++;
   }
   if(all[i].e < all[j].e){
    tempRankE++;
   }
   if(all[i].a < all[j].a){
    tempRankA++;
   }
  }
  int result[2];
  countBestRank(result,tempRankC+1,tempRankM+1,tempRankE+1,tempRankA+1);
  all[i].subject = result[0];
  all[i].rank = result[1];
 }
 for(int i = 0;i < requestStus;i++){
  bool is = true;
  if(i != 0){
   printf("\n");
  }
  for(int j = 0;j < students;j++){
   if(request[i].compare(all[j].stuNum) == 0){
    printf("%d %c",all[j].rank,all[j].subject);
    is = false;
    break;
   }
  }
  if(is == true){
   printf("N/A");
  }
 }
 return 0;
}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP