Java Scanner输入优化

文章目录

前言

以前的老玩意,没发~

题目

这个没什么好说的,还要从一道蓝桥杯的题目来说。

资源限制

时间限制:1.0s 内存限制:256.0MB

问题描述

JOE有一个长度为n的数组A,已知其中至少有一半以上的元素相同,现在JOE想知道数组的众数。

输入格式

第一行,一个数n。
  第二行,n个正整数,表示A数组。

输出格式

一个数,表示这n个数的众数。

样例输入

5
1 1 2 1 2

样例输出

1

数据规模和约定

30% n <= 10^4,数值 <= 10^8
  60% n <= 10^7,数值 <= 10^6
  100% n <= 10^7,数值 <= 10^8

本来这题挺简单的,直接扫描即可的。


import java.io.BufferedInputStream;
import java.util.Scanner;

public class Main {
    
    
        public static void main( String[ ] args) {
    
    
        Scanner scanner = new Scanner(new BufferedInputStream(System.in));
        Long n = scanner.nextLong();
        Long res = 1L;
        int result = scanner.nextInt();
        for(Long i = 1L;i<=n&res<=n;i++){
    
    
            int temp = scanner.nextInt();
            if(res == 0) result = temp;
            res += temp == result?1:-1;
        }
        System.out.println( result);
        }

}

但是对不起,60分,10个测试案例只过了6个
原因很简单,那个scanner不行呀。

解决方案就是使用 Buffer
类似重新Scanner的方法。

这里的话我在一个竞赛网站找到了帮我封装好的类


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


/** Class for buffered reading int and double values */
class Reader {
    
    
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
    
    
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
    
    
        while (!tokenizer.hasMoreTokens()) {
    
    
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
    
    
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
    
    
        return Double.parseDouble(next());
    }
}

问题解决

那么在题目里面是这样的


import java.io.*;
import java.util.Scanner;
import java.util.StringTokenizer;


/** Class for buffered reading int and double values */
class Reader {
    
    
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
    
    
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
    
    
        while (!tokenizer.hasMoreTokens()) {
    
    
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
    
    
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
    
    
        return Double.parseDouble(next());
    }
}
public class Main {
    
    
        public static void main( String[ ] args) throws IOException {
    
    
        Reader.init(System.in);
        int n = Reader.nextInt();
        int res = 1;
        int result = Reader.nextInt();
        for(int i = 1;i<=n&res<=n;i++){
    
    
            int temp = Reader.nextInt();
            if(res == 0) result = temp;
            res += temp == result?1:-1;
        }
        System.out.println( result);
        }

}

就是这样的,问题解决,搞了半天是java的 scanner不行!我找了半天资料,看别人的解法,可是想来想去都不可能是算法的问题(我还尝试了多线程方案,反而更不行,超内存,还要锁)而且无论如何其实发现都要读取百万次,所以就想是不是别人的问题比如scanner,结果一查好家伙。果然没有经历实战开发是不行的!

猜你喜欢

转载自blog.csdn.net/FUTEROX/article/details/123483699