1032(17)

初步判断,这一题是送分题,赶紧做出来。

思路:统计对应学校的总分,然后进行比较

一个超时,扣三分,数组还是不能多用。

import java.util.HashSet;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int[][] stu=new int[n][2];
        HashSet<Integer> col=new HashSet<Integer>();
        for (int i=0;i<n;i++){
                stu[i][0]=sc.nextInt();
                stu[i][1]=sc.nextInt();
                col.add(stu[i][0]);
        }
        /*需要新建两个数组
         * 对一个存放学校序号,元素不能重复,
         * 一个存放学校考试总分数,初始值为0*/
        Object[] o=col.toArray();
        int[] id=new int[col.size()];
        int[] score=new int[col.size()];
        for (int i=0;i<col.size();i++){
            id[i]=(int)(o[i]);
            score[i]=0;
            for (int j=0;j<n;j++){
                if (id[i]==stu[j][0]){
                    score[i]=score[i]+stu[j][1];
                }
            }
        }
        //获取score中最大元素
        int max=0;
        for (int i=0;i<score.length;i++){
            if(max<score[i]){
                max=score[i];
            }
        }
        for (int i=0;i<score.length;i++){
            if(score[i]==max){
                System.out.println(id[i]+" "+score[i]);
            }
        }


    }
}

 

posted @ 2018-10-15 23:00  博客园机器人  阅读(147)  评论(0)    收藏  举报