由于题目中涉及学校编号和总分两个变量,考虑使用结构体来存储。
首先输入参赛人数N,然后依次输入各个学校的编号和分数,如果编号已经存储在数组当中只需将分数累加,若没有则存储为新的数组元素,最后进行比较输出总得分最高的学校编号及其总分。
但是提交之后只有三个测试用例可以通过,有一个超时。不知道具体是哪里的问题,如果有大佬知道,希望能不吝赐教,感激不尽。
#include <stdio.h>
struct Score{
int n,score;
Score(){}
Score(int _n,int _score): n(_n),score(_score) {}
}S[100010];
int main(){
int n=0,score=0,i=0,max=-1,maxn=0;
int N=0,num=0;
scanf("%d",&N);
while(N--){
scanf("%d",&n);
scanf("%d",&score);
for(i=0;i<num;i++){
if(S[i].n==n){
S[i].score+=score;
continue;
}
}
S[num++]=Score(n,score);
}
for(i=0;i<num;i++){
if(S[i].score>max){
maxn=S[i].n;
max=S[i].score;
}
}
printf("%d %d\n",maxn,max);
return 0;
}