输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号、及其比赛成绩(百分制),中间以空格分隔。
在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
6<br/>3 65<br/>2 80<br/>1 100<br/>2 70<br/>3 40<br/>3 0
2 150
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
/**
* 挖掘机技术哪家强
* 题目描述
* 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强
* 的那个学校。
* 输入描述:
* 输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所
* 代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。
* 输出描述:
* 在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
* 输入例子:
* 6
* 3 65
* 2 80
* 1 100
* 2 70
* 3 40
* 3 0
* 输出例子:
* 2 150
*
* @author shijiacheng
* @date 2018/1/29
*/
public class B1022TheBestSchool {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < N; i++) {
int key = sc.nextInt();
int value = sc.nextInt();
int score = 0;
if (map.containsKey(key)) {
score += map.get(key) + value;
} else {
score = value;
}
map.put(key, score);
}
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
int value = -1;
int key = -1;
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
if (entry.getValue() > value) {
value = entry.getValue();
key = entry.getKey();
}
}
System.out.println(key + " " + value);
}
}
#include <iostream> #include <vector> #include <map> using namespace std; int main() { int N; cin >> N; vector<int> vec(N,0); map<int, int> map_count; for(int i =0; i<N; i++){ int val1,val2; cin >> val1 >> val2; map<int,int>::iterator it = map_count.find(val1); if(it == map_count.end()) map_count.insert(pair<int,int> (val1,val2)); else map_count[val1] += val2; } map<int,int>::iterator it = map_count.begin(); int number = 0,max = 0; for(; it!= map_count.end(); it++) if(it->second > max){ number = it->first; max = it->second; } cout << number << ' ' << max << endl; return 0; }
#include<iostream> using namespace std; int n,a,b,score[100001]={0},maxnum,maxscore=0; int main() { cin>>n; for(int i=0;i<n;i++) { cin>>a>>b; score[a]+=b;//将分数加到编号为a的学校的总分里 if(score[a]>maxscore) { maxnum=a;//当前分数最大的学校编号 maxscore=score[a];//当前的最大总分 } } cout<<maxnum<<' '<<maxscore<<endl; return 0; }
#Python保存为字典就好,然后使用字典的值进行排序 while True: try: num = int(input()) markDict = dict() for i in range(num): temp = input().split() if temp[0] not in markDict: #学校编号不在字典中,创建一个 markDict[temp[0]] = int(temp[1]) else: markDict[temp[0]] += int(temp[1]) #如果该学校编号已经存在,把成绩加进去 result = sorted(markDict.items(),key=lambda x:x[1]) print("%s %d" % result[-1]) except Exception: break
import java.util.Scanner; import java.util.TreeMap; import java.util.Map.Entry; import java.util.Map; import java.util.List; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ Map<Integer,Integer> map = new TreeMap<>(); int n = sc.nextInt(); for(int i =0;i<n;i++){ int key = sc.nextInt(); int value = sc.nextInt(); if(map.containsKey(key)){ map.put(key,map.get(key)+value); }else{ map.put(key,value); } } List<Map.Entry<Integer,Integer>> lt = new ArrayList(map.entrySet()); Collections.sort(lt,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Entry<Integer,Integer> cp1,Entry<Integer,Integer> cp2){ if(cp1.getValue() > cp2.getValue()){ return -1; } return 1; } }); System.out.println(lt.get(0).getKey()+" "+lt.get(0).getValue()); } } }
import java.util.*; public class Main{ public static void main(String [] args){ Scanner sc = new Scanner(System.in); int a = 0,b = 0, id = 0, max = 0; int n = sc.nextInt(); HashMap<Integer,Integer> map = new HashMap<Integer, Integer>(); for(int i = 0; i < n ; i++){ a = sc.nextInt(); b = sc. nextInt(); if(map.containsKey(a)) b = map.get(a) + b; map.put(a, b); if(max < b){ max = b; id = a; } } System.out.println(id + " " +max); } }
import java.util.*; public class Main{ public static void main(String []args){ Scanner in=new Scanner(System.in); int N=in.nextInt(); int Score[]=new int[100001]; int max=0; int index=0; for(int i=0;i<N;i++){ int n=in.nextInt(); Score[n]+=in.nextInt(); if(Score[n]>max){ max=Score[n]; index=n; } } System.out.println(index+" "+max); } }
import java.util.*; public class Main{ public static void main(String... args){ try(Scanner in = new Scanner(System.in)){ int n = in.nextInt(); //表示组数 Map<Integer,Integer> map = new HashMap<>(); for(int i = 0; i < n; i++){ int key = in.nextInt(); int val = in.nextInt(); Integer cnt = map.get(key) ; //记录每个学校的技术分数 if(cnt == null){ map.put(key,val); }else{ map.put(key,cnt + val); } } int key = 0;//记录最大的key int val = Integer.MIN_VALUE; for(Map.Entry<Integer,Integer> kv:map.entrySet()){ if(kv.getValue() > val){ key = kv.getKey(); val = kv.getValue(); } } System.out.println(key +" "+ val); } } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int score[N]; int main(){ int n,a,b,resid = 0; scanf("%d",&n); for(int i = 1; i <= n; i++){ scanf("%d%d",&a,&b); score[a] += b ; if(score[a]>score[resid]) resid = a; } printf("%d %d\n",resid,score[resid]); return 0; }
#include <iostream> using namespace std; int main(){ int n,i; cin>>n; int id[n],score[n]; int id_max=0; int sum[100001];//sum[0]为暂存标记 for(i=0;i<100001;i++){ sum[i]=0; } for(i=0;i<n;i++){ cin>>id[i]>>score[i]; sum[id[i]]+=score[i]; if(sum[id[i]]>sum[0]){ sum[0]=sum[id[i]]; id_max=id[i]; } } cout<<id_max<<" "<<sum[0]; return 0; }
//想问下pat最后一个测试点为什么会超时 #include<stdio.h> #include<stdlib.h> int main() { int school[10000] = { 0 }, score[10000], count,temp1,temp2,max,sch,i,t; scanf("%d", &count); for (i = 0; i < count; i++) { scanf(" %d %d", &temp1, &temp2); for (t = 0; t < 10000; t++) { if (school[t] == temp1) { score[t] = score[t] + temp2; break; } if (school[t] == 0) { school[t] = temp1; score[t] = temp2; break; } } } max = score[0]; sch = school[0]; for (i = 0; i < count; i++) { if (school[i] == 0) { printf("%d %d", sch, max); return 0;; } if (score[i] > max) { max = score[i]; sch = school[i]; } } printf("%d %d", sch, max); return 0; }//想问下pat最后一个测试点为什么会超时....从java过来的
#include<iostream> #include<string.h> using namespace std; int main(){ int N; int max=0,id=0; int num[100000],temp=0,grade; memset(num,0,sizeof(num)); cin>>N; for(int i=0;i<N;++i){ cin>>temp>>grade; num[temp]+=grade; if(num[temp]>max){ max=num[temp]; id=temp; } } cout<<id<<" "<<max<<endl; return 0; }
#include<iostream> using namespace std; int main(){ int N,i,max=0,d; cin>>N; int *a=new int[N]; int *b=new int[N]; int c[100000]={0}; for(i=0;i<N;i++){ cin>>a[i]; cin>>b[i]; } for(i=0;i<N;i++){ c[a[i]]=c[a[i]]+b[i]; } for(i=0;i<N;i++){ if(c[a[i]]>max)max=c[a[i]]; } for(i=0;i<N;i++){ if(c[a[i]]==max)d=a[i]; } cout<<d<<" "<<max; delete[] a; delete[] b; return 0; }
//PAT官网的时间限制是200ms,这种方法在官网有测试点超时,有改进想法的同学欢迎私信告诉我。 import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int[] school = new int[100010]; int maxid=0,maxscore=0; Arrays.fill(school, 0); for (int i = 0; i < N; i++) { int id=in.nextInt(); int score=in.nextInt(); school[id] += score; if (school[id]>maxscore) { maxid=id; maxscore=school[id]; } } System.out.println(maxid + " " + maxscore); in.close(); } }
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] player = new int[100005]; Arrays.fill(player,0); int N = in.nextInt(); for(int i = 0;i<N;i++){ player[in.nextInt()] += in.nextInt(); } int max = 0,id = 0; for(int i = 0;i<100005;i++){ if(player[i]>max){ max = player[i]; id = i; } } System.out.println(id+" "+player[id]); } }
using namespace std;
}