Java快速输入输出

需要的头文件有

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

在主函数中使用StreamTokenizer和PrintWriter类

StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));

输入结束标志参数为

StreamTokenizer.TT_EOF

注意读入的时候要调用nextToken()函数,输出之后调用flush()函数,以hdu 1029为例

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

public class Main {
    public static void main(String[] args) throws IOException {
        StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
        while(in.nextToken()!=StreamTokenizer.TT_EOF) {
            int n=(int)in.nval;//读入一个整数
            int []c=new int[100000];
            int ans=0;
            for(int i=0;iin.nextToken();
                int x=(int)in.nval;
                ++c[x];
                if(c[x]>=(n+1)/2) ans=x;
            }
            out.println(ans);
            out.flush();//输出一次之后要刷新
        }
    }
}

你可能感兴趣的:(输入输出,输入输出,Java)