PAT-L1-79 Good JAVA Realization of Ladder Competition

topic details

Ladders are a good game. The kind-hearted proposition team hopes to control the difficulty of the questions within a certain range, so that every participating student has a problem that can be solved, and the best students have to work very hard to get a high score.

So the proposition group first divided the programming ability into 10^6
levels (too crazy, this is fake), and then investigated the programming ability of each participating student. Now please write a program to find out the minimum and maximum ability values ​​of all participating students, and give it to the proposition group as a reference for the problem.

Input format:
Enter a positive integer N (≤2×10^4 ) in the first line, which is the total number of participating students. The next line gives N positive integers not exceeding 10^6, which are the ability values ​​of the participating students.

Output format:
The first line outputs the minimum ability value of all participating students and the number of students with this ability value. The second line outputs the maximum ability value of all participating students and the number of students with this ability value. Numbers in the same row are separated by 1 space, and there must be no extra spaces at the beginning and end of the line.

Input sample:

10 86 75 233 888 666 75 886 888 75 666

Sample output:

75 3 888 2


analyze
It should be noted that there is a time limit for this question. Within 200ms, it is easy to time out in java. The reason is that the number of students input may reach the 4th power of 10. There is a quite large number in the test case. If the input is not processed , timeout is inevitable

For the processing of input data, there is StreamTokenizer in addition to Scanner and BufferedReader

The java.io.StreamTokenizer class takes an input stream and parses it into "tokens", allowing tokens to be read one at a time. Stream tokenizers recognize identifiers, numbers, quoted strings, and various comment styles.It is more efficient than BufferedReader for input and acquisition of large amounts of data

The initialization in the code is as follows

StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

The commonly used methods are as follows

double nval - if the current token is a number, this field contains the value of that number

int nextToken() - This method parses the next token from this tokenizer's input stream.

The two-piece set for obtaining data with StreamTokenizer is : nextToken() and (int)sc.nval

Note: Don’t write this nval wrongly. I wrote it as navl before, but I didn’t find the problem for a long time. A lesson learned from blood and tears

//声明StreamTokenizer
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//先获取下一个数
sc.nextToken();
//把获取的数字给转换为int型
int num = (int)sc.nval;

The specific code is as follows

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

public class Main {
    
    
	public static void main(String[] args) throws IOException {
    
    
        StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        //先获取下一个数
        sc.nextToken();
        //把获取的数字给转换为int型
        int num = (int)sc.nval;
        //初始最大,最小值
        int max = 0, min = 99999;
        //分别为最大值的个数和最小值的个数
        int m = 0, n = 0;
        //临时变量用于存储获取的值
        int temp;
        for(int i = 0; i < num; i++){
    
    
            //获取输入流中的数
            sc.nextToken();
            temp = (int) sc.nval;
            
            if(temp > max){
    
    
                max = temp;
                m = 1;
            }else if(temp == max){
    
    
                m++;
            }
            
            if(temp < min){
    
    
                min = temp;
                n = 1;
            }else if(temp == min){
    
    
                n++;
            }
        }
        System.out.println(min + " " + n);
        System.out.println(max + " " + m);
    }
}

Guess you like

Origin blog.csdn.net/giveupgivedown/article/details/129881364