UVa 的 3n+1 挑战

发布于 2024-10-16 07:19:37 字数 2714 浏览 1 评论 0原文

我在运行“编程挑战”中的“3n+1 问题”时遇到问题

我已经尝试了在 google 上找到的所有 Java 解决方案(甚至是 Stack Overflow 上的解决方案),但没有一个有效,它们都报告“错误答案”。我还找到了一个可行的 C++ 解决方案,将其翻译为 Java,并且得到了同样的结果:“错误答案”。

我正在使用 Java 编程挑战提交中的模板,我可以发誓我的算法是正确的,我能想到的唯一可能的问题是在读取输入或写入输出的代码中,但我无法弄清楚。这是我的代码,任何帮助将不胜感激:

class myStuff implements Runnable {

    @Override
    public void run() {
        String line = Main.ReadLn(128);
        while (line != null) {
            process(line);
            line = Main.ReadLn(128);
        }
    }

    private void process(String line) {

        String[] data = line.split("\\s+");

        if (data.length == 2) {
            int low = Integer.parseInt(data[0]);
            int high = Integer.parseInt(data[1]);
            int max = low < high ? findMax(low, high) : findMax(high, low);
            System.out.println(low + " " + high + " " + max);
        }

    }

    private int findMax(int low, int high) {
        int max = Integer.MIN_VALUE;
        for (int i = low; i <= high; i++) {
            int length = cycleLength(i);
            if (length > max)
                max = length;
        }
        return max;
    }

    private int cycleLength(int i) {

        long n = i;
        int length = 1;

        while (n > 1) {
            n = ((n & 1) == 0) ? n >> 1 : 3*n + 1;
            length++;
        }

        return length;

    }

}

// java program model from www.programming-challenges.com
class Main implements Runnable {
    static String ReadLn(int maxLength) { // utility function to read from
        // stdin, Provided by Programming-challenges, edit for style only
        byte line[] = new byte[maxLength];
        int length = 0;
        int input = -1;
        try {
            while (length < maxLength) { // Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n'))
                    break; // or untill end of line ninput
                line[length++] += input;
            }

            if ((input < 0) && (length == 0))
                return null; // eof
            return new String(line, 0, length);
        } catch (java.io.IOException e) {
            return null;
        }
    }

    public static void main(String args[]) { // entry point from OS
        Main myWork = new Main(); // Construct the bootloader
        myWork.run(); // execute
    }

    @Override
    public void run() {
        new myStuff().run();
    }

}

I'm having trouble running the "3n+1 Problem" from the "Programming Challenges" book.

I've tried every solution in Java I could find on google (even the ones on Stack Overflow), and not a single one works, they all report a "Wrong answer". I also found a working C++ solution, translated it to Java, and same thing: "Wrong answer".

I'm using the template from Programming Challenges for Java submissions, and I could swear my algorithm is right, the only possible problem I can think of is in the code for reading the input or writing the output, but I can't figure it out. Here's my code, any help would be greatly appreciated:

class myStuff implements Runnable {

    @Override
    public void run() {
        String line = Main.ReadLn(128);
        while (line != null) {
            process(line);
            line = Main.ReadLn(128);
        }
    }

    private void process(String line) {

        String[] data = line.split("\\s+");

        if (data.length == 2) {
            int low = Integer.parseInt(data[0]);
            int high = Integer.parseInt(data[1]);
            int max = low < high ? findMax(low, high) : findMax(high, low);
            System.out.println(low + " " + high + " " + max);
        }

    }

    private int findMax(int low, int high) {
        int max = Integer.MIN_VALUE;
        for (int i = low; i <= high; i++) {
            int length = cycleLength(i);
            if (length > max)
                max = length;
        }
        return max;
    }

    private int cycleLength(int i) {

        long n = i;
        int length = 1;

        while (n > 1) {
            n = ((n & 1) == 0) ? n >> 1 : 3*n + 1;
            length++;
        }

        return length;

    }

}

// java program model from www.programming-challenges.com
class Main implements Runnable {
    static String ReadLn(int maxLength) { // utility function to read from
        // stdin, Provided by Programming-challenges, edit for style only
        byte line[] = new byte[maxLength];
        int length = 0;
        int input = -1;
        try {
            while (length < maxLength) { // Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n'))
                    break; // or untill end of line ninput
                line[length++] += input;
            }

            if ((input < 0) && (length == 0))
                return null; // eof
            return new String(line, 0, length);
        } catch (java.io.IOException e) {
            return null;
        }
    }

    public static void main(String args[]) { // entry point from OS
        Main myWork = new Main(); // Construct the bootloader
        myWork.run(); // execute
    }

    @Override
    public void run() {
        new myStuff().run();
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-10-23 07:19:37

解决了。好的,对于初学者来说,该网站 http://programming-challenges.com 目前肯定无法用于 Java 提交(他们正在做一些现在是一种服务器迁移)。我尝试了备用网站 http://uva.onlinejudge.org ;那一个正在正确处理 Java 提交。

但无论如何,我上面的代码中有一个错误 - 这行修复了它:

String[] data = line.trim().split("\\s+");

输入数据总是很混乱 - 额外的空格、空行等,任何试图解析输入的人都应该假设这一点。

Solved. OK, for starters the site http://programming-challenges.com definitely is not working right now for Java submissions (they're doing some kind of server migration now). I tried the alternate site http://uva.onlinejudge.org ; that one is processing Java submissions correctly.

But anyway, I had a bug in my code above - this line fixes it:

String[] data = line.trim().split("\\s+");

The input data will always be messy - extra spaces, empty lines, etc. and anyone trying to parse the input should assume this.

七月上 2024-10-23 07:19:37

首页>在线评判>提交规范

此链接可能会有所帮助

读取输入的示例代码来自:http: //online-judge.uva.es/problemset/data/p100.java.html

这是 stackoverflow 的另一个链接:https://stackoverflow.com/a/14632770/1060656

我认为UVA判断中最重要的是1)得到的输出完全一样,最后没有多余的行。 2)我假设,永远不会抛出异常,只是返回或中断,并且外部边界参数没有输出。
3)输出区分大小写 4)输出参数应保持空间,如问题所示

Home>Online Judge > submission Specifications

this link might help

Sample code to read input is from : http://online-judge.uva.es/problemset/data/p100.java.html

here is one more link for stackoverflow : https://stackoverflow.com/a/14632770/1060656

I think the most important thing in UVA judge is 1) Get the output Exactly same , No Extra Lines at the end . 2) I am assuming , Never throw exception just return or break with No output for Outside boundary parameters.
3)Output is case sensitive 4)Output Parameters should Maintain Space as shown in problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文