PAT乙级1032

在这里插入图片描述

//1032
#include <iostream>
#include <map> 
using namespace std;
int main(){
	int n;
	cin>>n;
	map<int,int> m;
	for(int i=0;i<n;i++){
		int school,score;
		cin>>school>>score;
		m[school]+=score;
	}
	int max=m.begin()->second;
	int team;
	for(auto it=m.begin();it!=m.end();it++){
		if(it->second>max){
			team=it->first;
			max=it->second;
		}
	}
	cout<<team<<" "<<max;
	return 0;
}

答案有错误,有两个测试过不了
将team赋值1后正确,原因可能是最高分是1号,不会执行下面的判断语句,所以team就没有值

下面是vector的做法

//代码是照抄liuchuo的
#include <iostream>
#include <vector>
using namespace std;
int main() {
    int N;
    cin >> N;
    vector<int> a(N + 1);
    int num, score;
    for (int i = 0; i < N; i++) {
        cin >> num >> score;
        a[num] += score;
    }
    int max = a[1], t = 1;
    for (int i = 2; i <= N; i++) {
        if (max < a[i]) {
            max = a[i];
            t = i;
        }
    }
    cout << t << " " << max;
    return 0;
}
[liuchuo](https://github.com/liuchuo/PAT/blob/master/BasicLevel_C++/1032.%20%E6%8C%96%E6%8E%98%E6%9C%BA%E6%8A%80%E6%9C%AF%E5%93%AA%E5%AE%B6%E5%BC%BA%2820%29.cpp)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值