PAT B1032 挖掘机技术哪家强

版权声明:站在巨人的肩膀上学习。 https://blog.csdn.net/zgcr654321/article/details/83270385

本地编译环境

系统:win7 64位;
C/C++环境:Clion2018,编译器cygwin。
Java环境:IDEA2018,编译器jdk-8u172。
Python环境:pycharm,编译器Anaconda3:Python3.6。

PTA提交

注意:
由于Java和Python语言运行较慢,这里只保证C/C++解法一定满足题目的运行时间限制。

C/C++解法:编译器C++(g++)/31ms。
Java解法:编译器Java(openjdk)/112ms,测试点3运行超时。
Python解法:编译器Python(python 3)/24ms,测试点3运行超时。

题目

本题也是算法笔记3.1中例题。

为了用事实说明挖掘机技术到底哪家强, PAT 组织了一场挖掘机技能大赛。请根据比赛结果统计出技术最强的那个学校。
输入格式:
在第1行给出不超过10的5次方的正整数N, 即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1 开始连续编号)及其比赛成绩(百分制),中间以空格分隔。
输出格式:
在一行中给出总得分最高的学校的编号及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
注意:
注意每个学校最后比较的是总分,需要把同学校的参赛者的分数相加起来。

C/C++解法

#include <stdio.h>

class Solution {
public:
	int count_Score() {
		int N;
		scanf("%d", &N);
		int *school = new int[N + 1]();
		int schoolID=0, score=0;
		int max_schoolID = 0, max_score = 0;
		for (int i = 1; i <= N; i++) {
			scanf("%d%d", &schoolID, &score);
			school[schoolID] += score;
			if (school[schoolID] > max_score) {
				max_schoolID = schoolID;
				max_score = school[schoolID];
			}
		}
		printf("%d %d\n",max_schoolID,max_score);
		return 0;
	}
};

int main() {
	Solution s;
	s.count_Score();
	return 0;
}

Java解法

import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class Main {
	public static class Solution {
		int countScore() {
			Scanner sc = new Scanner(System.in);
			int N = sc.nextInt();
			int maxSchoolID = 0, maxScore = 0;
			Map<Integer, Integer> school = new HashMap();
			for (int i = 1; i <= N; i++) {
				int schoolID = sc.nextInt();
				int score = sc.nextInt();
				if (school.containsKey(schoolID)) {
					int temp = school.get(schoolID);
					temp = temp + score;
					school.put(schoolID, temp);
				} else
					school.put(schoolID, score);
				if (school.get(schoolID) > maxScore) {
					maxScore = school.get(schoolID);
					maxSchoolID = schoolID;
				}
			}
			sc.close();
			System.out.printf("%d %d\n",maxSchoolID,maxScore);
			return 0;
		}
	}

	public static void main(String[] args) {
		Solution s = new Solution();
		s.countScore();
	}
}

Python3解法

class Solution:
	def countscore(self):
		N = int(input())
		school = {}
		maxSchoolID, maxScore = 0, 0
		for i in range(N):
			schoolID, score = (int(x) for x in input().split())
			if schoolID in school.keys():
				temp = school.get(schoolID)
				temp = temp + score
				school.update({schoolID: temp})
			else:
				school.setdefault(schoolID, score)
			if school[schoolID] > maxScore:
				maxSchoolID = schoolID
				maxScore = school[schoolID]
		print(maxSchoolID, maxScore)


s = Solution()
s.countscore()

猜你喜欢

转载自blog.csdn.net/zgcr654321/article/details/83270385