Which is the best excavator Technology (20 points) java

Posted by imcookie on Sat, 30 Nov 2019 13:04:46 +0100

In order to show which excavator technology is the best, PAT organized a excavator skills competition. Now please count the most skilled school according to the result of the competition.

Input format:

Enter a positive integer N in line 1 that does not exceed 10 ^ 5, that is, the number of participants. Then N lines, each line gives the information and scores of a contestant, including the number of the school it represents (consecutively numbered from 1), and its competition scores (percentage system), separated by spaces.

Output format:

Give the number of the school with the highest total score and its total score in one line, separated by spaces. The questions guarantee that the answers are unique and there is no parallel.
Input example:

6
3 65
2 80
1 100
2 70
3 40
3 0

Output example:

2 150

Think
The array I used is stored here. The school number is the array subscript, and the score is the corresponding subscript value. Note that the school number starts at 1.

code


import java.io.*;

public class which school is proficient in excavator technology {

    public static void main(String[] args) throws IOException {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        in.nextToken();
        int n = (int) in.nval,max = 0,len,sch = 0;
        int[] score = new int[n];
        while(0 < n--) {
            in.nextToken();
            int schoo = (int) in.nval;
            in.nextToken();
            int sco = (int) in.nval;
            score[schoo] += sco;
        }
        len = score.length;
        for(int i = 1 ; i < len ; i++) {
            if(score[i] > max) {
                max = score[i];
                sch = i;
            }
        }
        System.out.print(sch + " " + max);
    }
}

Unfortunately, java code can't run pta, so use C + + code, and the test passed

    #include<iostream>
    #include<cstdio>
    using namespace std;
    int a[100001]={0};
    int main() {
      int n,bianhao,score,max=0,j;
      cin>>n;
      while(n--){
        cin>>bianhao>>score;
        a[bianhao] += score;
      }
      for(int i=0;i<100000;i++) {
        if(a[i]>max) {
          max = a[i];
          j = i;
        }
      }
      cout<<j<<" "<<max; return 0;
    }

Topics: Java