(PAT (Advanced Level) Practice)1012 The Best Rank (25 分)(java实现)

java实现比较容易超时,因此使用了StringBuilder,需要多运行几次

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;


public class pta_1012 {
	static ArrayList<Integer> AList = new ArrayList<>();
	static ArrayList<Integer> CList = new ArrayList<>();
	static ArrayList<Integer> MList = new ArrayList<>();
	static ArrayList<Integer> EList = new ArrayList<>();
	static StringBuilder res = new StringBuilder();
	static class student{
		private int A ;
		private int M ;
		private int E ;
		private int C ;
		public student(int C, int M, int E, int A) {
			this.A = A;
			this.M = M;
			this.C = C;
			this.E = E;
		}
		public void showBestRank() {
			int n = AList.size();
			int bestRank = n - AList.lastIndexOf(A);
			String Grade = "A";
			int cRank = n - CList.lastIndexOf(C);
			int mRank = n - MList.lastIndexOf(M);
			int eRank = n - EList.lastIndexOf(E);
			if(cRank < bestRank) {
				bestRank = cRank;
				Grade = "C";
			}
			if(mRank < bestRank) {
				bestRank = mRank;
				Grade = "M";
			}
			if(eRank < bestRank) {
				bestRank = eRank;
				Grade = "E";
			}
			res.append(bestRank).append(" ").append(Grade).append("\n");
		}
	}
	static int str2num(String str) {
		return Integer.parseInt(str);
	}
	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String[] in = bf.readLine().split(" ");
		int n = str2num(in[0]);
		int m = str2num(in[1]);
		HashMap<Integer, student> map = new HashMap<>();
		for(int i = 0; i< n; i++) {
			in = bf.readLine().split(" ");
			int cGrade = str2num(in[1]);
			int mGrade = str2num(in[2]);
			int eGrade = str2num(in[3]);
			int aGrade = (cGrade+mGrade+eGrade)/3;
			AList.add(aGrade);
			CList.add(cGrade);
			MList.add(mGrade);
			EList.add(eGrade);
			map.put(str2num(in[0]),new student(cGrade,mGrade,eGrade,aGrade));
		}
		Collections.sort(AList);
		Collections.sort(CList);
		Collections.sort(MList);
		Collections.sort(EList);
		for(int i = 0; i < m; i++) {
			int input = str2num(bf.readLine());
			if(map.containsKey(input)) {
				map.get(input).showBestRank();
			}else {
				res.append("N/A\n");
			}
		}
		System.out.println(res.toString().substring(0,res.length() - 1));
	}

}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值