UVa的3n + 1挑战 [英] 3n+1 challenge at UVa

查看:111
本文介绍了UVa的3n + 1挑战的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在编程挑战中运行3n + 1问题书籍

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

我尝试过在谷歌上找到的所有Java解决方案(即使是Stack Overflow上的解决方案) ),而不是一个人工作,他们都报告错误的答案。我还找到了一个可用的C ++解决方案,将其翻译成Java,同样的事情:错误的答案。

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".

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

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();
    }

}


推荐答案

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

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.

这篇关于UVa的3n + 1挑战的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆