Java的快速I/O

时间:2021-12-21 12:26:02

一、利用BufferedReader和StringTokenizer
// Working program with FastReader
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Scanner;
importjava.util.StringTokenizer;

publicclass Main
{
staticclass FastReader
{
BufferedReader br;
StringTokenizer st;

    publicFastReader()
    {
        br = newBufferedReader(new
                 InputStreamReader(System.in));
    }

    String next()
    {
        while(st == null|| !st.hasMoreElements())
        {
            try
            {
                st = newStringTokenizer(br.readLine());
            }
            catch(IOException  e)
            {
                e.printStackTrace();
            }
        }
        returnst.nextToken();
    }

    intnextInt()
    {
        returnInteger.parseInt(next());
    }

    longnextLong()
    {
        returnLong.parseLong(next());
    }

    doublenextDouble()
    {
        returnDouble.parseDouble(next());
    }

    String nextLine()
    {
        String str = "";
        try
        {
            str = br.readLine();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        returnstr;
    }
}

publicstatic void main(String[] args)
{
    FastReader s=newFastReader();
    intn = s.nextInt();
    intk = s.nextInt();
    intcount = 0;
    while(n-- > 0)
    {
        intx = s.nextInt();
        if(x%k == 0)
           count++;
    }
    System.out.println(count);
}

}

二、利用DataInputStream,比使用BufferedReader快
// Working program using Reader Class
importjava.io.DataInputStream;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.util.Scanner;
importjava.util.StringTokenizer;

publicclassMain
{
staticclassReader
{
finalprivateint BUFFER_SIZE = 1<<16;
privateDataInputStream din;
privatebyte[] buffer;
privateintbufferPointer, bytesRead;

    publicReader()
    {
        din = newDataInputStream(System.in);
        buffer = newbyte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    publicReader(String file_name) throwsIOException
    {
        din = newDataInputStream(newFileInputStream(file_name));
        buffer = newbyte[BUFFER_SIZE];
        bufferPointer = bytesRead = 0;
    }

    publicString readLine() throwsIOException
    {
        byte[] buf = newbyte[64];// line length
        intcnt = 0, c;
        while((c = read()) != -1)
        {
            if(c == '\n')
                break;
            buf[cnt++] = (byte) c;
        }
        returnnewString(buf, 0, cnt);
    }

    publicintnextInt() throwsIOException
    {
        intret = 0;
        bytec = read();
        while(c <= ' ')
            c = read();
        booleanneg = (c == '-');
        if(neg)
            c = read();
        do
        {
            ret = ret * 10+ c - '0';
        } while((c = read()) >= '0'&& c <= '9');

        if(neg)
            return-ret;
        returnret;
    }

    publiclongnextLong() throwsIOException
    {
        longret = 0;
        bytec = read();
        while(c <= ' ')
            c = read();
        booleanneg = (c == '-');
        if(neg)
            c = read();
        do{
            ret = ret * 10+ c - '0';
        }
        while((c = read()) >= '0'&& c <= '9');
        if(neg)
            return-ret;
        returnret;
    }

    publicdoublenextDouble() throwsIOException
    {
        doubleret = 0, div = 1;
        bytec = read();
        while(c <= ' ')
            c = read();
        booleanneg = (c == '-');
        if(neg)
            c = read();

        do{
            ret = ret * 10+ c - '0';
        }
        while((c = read()) >= '0'&& c <= '9');

        if(c == '.')
        {
            while((c = read()) >= '0'&& c <= '9')
            {
                ret += (c - '0') / (div *= 10);
            }
        }

        if(neg)
            return-ret;
        returnret;
    }

    privatevoidfillBuffer() throwsIOException
    {
        bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
        if(bytesRead == -1)
            buffer[0] = -1;
    }

    privatebyteread() throwsIOException
    {
        if(bufferPointer == bytesRead)
            fillBuffer();
        returnbuffer[bufferPointer++];
    }

    publicvoidclose() throwsIOException
    {
        if(din == null)
            return;
        din.close();
    }
}

publicstaticvoid main(String[] args) throwsIOException
{
    Reader s=newReader();
    intn = s.nextInt();
    intk = s.nextInt();
    intcount=0;
    while(n-- > 0)
    {
        intx = s.nextInt();
        if(x%k == 0)
           count++;
    }
    System.out.println(count);
}

}