当前位置: 首页 > 知识库问答 >
问题:

超过时限在线法官

宰子琪
2023-03-14

这就是问题所在:

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

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个答案

侯沈义
2023-03-14

最后编码希望你发现这有用用了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));
        }
    }
} 
 类似资料:
  • 我成功地解决了Hackerrank上的一个问题,它通过了所有测试用例,但我得到了一个错误,时间限制超过。我猜如果我优化我的代码,它会起作用,但我想不出任何方法来使我的代码更有效率。 问题是:对大小为n的数组进行左旋操作会将数组的每个元素向左移动1个单位。例如,如果在数组[1,2,3,4,5]上执行两次左旋转,则该数组将变为[3,4,1,2]。 给定一个由n个整数和一个数字d组成的数组,在数组上执行

  • 截图显示了相同数据的三次贝塞尔模式和不三次贝塞尔模式的图表。如你所见,当立方贝塞尔模式是启用,线ECXEED的最小和最大值,我需要显示在图表作为限制线,它看起来非常糟糕。 有什么办法可以解决这个问题吗??

  • 我真的很困惑为什么我的Java代码不起作用,它给了黑客地球上的代码僧侣TLE。这里是指向1的链接 链接质疑第一个问题和尚和旋转 我不知道为什么它给了TLE我想这是一个无限循环。 现场的问题是- 蒙克和旋转蒙克喜欢对数组执行不同的操作,所以作为哈克地球学校的校长,他给他的新学生米什基布置了一个任务。Mishki将被提供一个大小为N的整数数组A和一个整数K,在这里她需要将数组向正确的方向旋转K步,然后

  • 我使用命令生成器在Jenkins服务器上运行单元测试。phar exec“codecept运行单元应用程序/模型”-vvv并获取以下错误: [Symfony\Component\Process\Exception\ProcessTimedOutException] 进程“codecept运行单元应用程序/模型”超过了1800秒的超时时间。 我如何修复它并允许测试花费更多的时间?谢谢

  • 但这并不能解决我的问题。我在我的分级中启用了multidex,因为没有它我会得到错误: com.android.dex.DexIndexOverflowException:方法ID不在[0,0xFFFF]:65536 所以这是一个解决方案,它在以前的Android Studio版本(也适用于公司的其他人,他们正在使用Android Studio1.4-2.0),但在我升级了我的Android St

  • 问题内容: 我有一个自动运行git clone /pull的脚本(这实际上发生在jenkinsCI中,但我的问题更笼统)。远程git服务器基于HTTPS。带有git客户端的计算机具有不稳定的DSL Internet连接,因此有时会重新连接并更改IP地址,从而丢失所有现有连接。当git客户端运行时连接失败时,客户端将永远不会成功,但也不会因超时而失败,因此我的脚本会挂断。 我想设置客户端,使其在一段

  • 我是DP的初学者,我正在尝试解决一个Leetcode问题-最大子数组:给定一个整数数组nums,找到具有最大和的连续子数组(至少包含一个数字),并返回其和。我知道这个问题已经有很多DP解决方案。但我想自己写一个DP解决方案,我在网上搜索了一些关于DP的介绍。以下是我使用的具体参考资料:https://stackoverflow.com/a/13538715/9982458.根据该答案,DP的运行时