Java的快速I/O

一、利用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);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值