通过对输入数据进行处理,从而来获得自己单科或者平均的最好成绩。
一开始,利用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;
}
|