时间限制超过在线判断

时间:2016-03-15 01:49:58

标签: java

这是问题所在:

  

输入   第一行输入将包含测试用例数T(1≤T≤50)。以下每个T   lines包含一个正整数N,其长度不超过80位。   产量   每个测试用例的输出将是包含最小回文的单行   大于或等于输入数字。   样本输入

2
42
321
     

示例输出

44
323

当我提交代码进行在线判断(3秒限制)时,我一直超出时间限制

  class Main {

static String ReadLn (int maxLg) 
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }


    static boolean isPalandriome(String s){
        String newString = "";
        for(int i =s.length()-1;i >= 0; i--){
            newString += s.charAt(i);
        }
        if(newString.equals(s))
            return true;
        else
            return false;
    }
    public static void main(String[] args) {

            BigInteger entredNumber;
            String input;
            input = Main.ReadLn(10);
            int tests = Integer.parseInt(input);
            List<BigInteger> numbers = new ArrayList<BigInteger>();
            for (int i =0;i<tests;i++)
        {
            input = Main.ReadLn(100);
            entredNumber = new BigInteger(input);
                numbers.add(entredNumber);

        }


            for(int i=0;i<tests;i++){
                BigInteger number = numbers.get(i);

                while(!isPalandriome(String.valueOf(number))){
                    number  = number.add(BigInteger.ONE);
                }
                System.out.println(number);

            }

    }
}

我无法在代码中找到需要花费太多时间的内容。

1 个答案:

答案 0 :(得分:0)

最后编码希望你发现这有用了0.10秒

import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        CustomReader cr = new CustomReader(1000000);
        int T = cr.nextInt(), fIndex, bIndex, fStartIndex, bStartIndex;
        StringBuilder output = new StringBuilder();
        byte[] input;
        boolean isAppend1 = false;
        for (int i = 0; i < T; i++) {
            input = cr.nextInput();
            fStartIndex = bStartIndex = cr.getCurrInputLength() / 2;
            isAppend1 = false;
            if (cr.getCurrInputLength() % 2 == 0) {
                bStartIndex--;
            }
            fIndex = fStartIndex;
            bIndex = bStartIndex;
            while (input[bIndex] == input[fIndex]) {
                if (bIndex - 1 < 0) {
                    break;
                } else {
                    bIndex--;
                    fIndex++;
                }
            }
            if (input[bIndex] > input[fIndex]) {
                while (bIndex >= 0) {
                    input[fIndex++] = input[bIndex--];
                }
            } else {
                if (input[bStartIndex] < 57) {
                    input[bStartIndex] = (byte) (input[bStartIndex] + 1);
                } else {
                    bIndex = bStartIndex;
                    while (bIndex >= 0 && input[bIndex] == 57) {
                        input[bIndex] = 48;
                        bIndex--;
                    }
                    if (bIndex >= 0) {
                        input[bIndex] = (byte) (input[bIndex] + 1);
                    } else {
                        input[0] = 49;
                        if (fStartIndex != bStartIndex) {
                            input[fStartIndex] = 48;
                            bStartIndex = fStartIndex;
                        } else {
                            input[fStartIndex + 1] = 48;
                            bStartIndex = fStartIndex = fStartIndex + 1;
                        }
                        isAppend1 = true;
                    }
                }
                while (bStartIndex > -1) {
                    input[fStartIndex++] = input[bStartIndex--];
                }
            }
            for (int j = 0; j < cr.getCurrInputLength(); j++) {
                output.append((char) input[j]);
            }
            if (isAppend1) {
                output.append("1");
            }
            output.append("\n");
        }
        System.out.print(output.toString());
        // genInput();
    }

    private static class CustomReader {
        private byte[] buffer;
        private byte[] currInput = new byte[1000000];
        private int currInputLength;
        private int currIndex;
        private int validBytesInBuffer;

        CustomReader(int buffSize) {
            buffer = new byte[buffSize];
        }

        public int nextInt() throws IOException {
            int value;
            byte b;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    break;
                }
            }
            value = b - 48;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    value = (value * 10) + (b - 48);
                } else {
                    break;
                }
            }
            return value;
        }

        public byte[] nextInput() throws IOException {
            byte b;
            this.currInputLength = 0;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    break;
                }
            }
            currInput[currInputLength++] = b;
            while (true) {
                b = getNextByte();
                if (b > 47 && b < 58) {
                    currInput[currInputLength++] = b;
                } else {
                    break;
                }
            }
            return this.currInput;
        }

        public int getCurrInputLength() {
            return this.currInputLength;
        }

        private byte getNextByte() throws IOException {
            if (currIndex == buffer.length || currIndex == validBytesInBuffer) {
                validBytesInBuffer = System.in.read(buffer);
                currIndex = 0;
            }
            return buffer[currIndex++];
        }
    }

    public static void genInput() {
        for (int i = 0; i < 100; i++) {
            System.out.println((int) (Math.random() * 1000000000));
        }
    }
}