0

Simply changing from Swap Method A to Swap Method B, the UVa judge goes from 'Wrong Answer' to 'Accepted'. Why is that? I thank you all in advance for your responses.

import java.io.IOException;
import java.util.StringTokenizer;

/**
 * @author Coder47 
 * Another solution to 3n+1 (UVa ID: 100)
 */
class Main {
    public static int[] cache = new int[1000000];
    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;

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

    public static void main(String[] args) {
        Main myWork = new Main();  // create a dynamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin() {
        String input;
        StringTokenizer idata;
        // corresponds to swap method A
        int a, b, temp, cycle, cycleMax;
        // corresponds to swap method B
        // int a, b, min, max, cycle, cycleMax;

        while ((input = Main.ReadLn(255)) != null)
        {
            idata = new StringTokenizer(input);
            a = Integer.parseInt(idata.nextToken());
            b = Integer.parseInt(idata.nextToken());
            // swap method A
            if (b < a) {
                temp = a;
                a = b;
                b = temp;
            }

            // swap method B
            // min = Math.min(a, b);
            // max = Math.max(a, b);
            cycleMax = 0;
            // corresponds to swap method A
            for (int i = a; i <= b; i++)
            // corresponds to swap method B
            // for ( int i = min; i <= max; i++)
            {
                cycle = calculateCycle(i);
                if (cycle > cycleMax) {
                    cycleMax = cycle;
                }
            }
            System.out.println (a + " " + b + " " + cycleMax);
        }
    }

    public int calculateCycle(int n) {
        return calculateCycleHelper(n, 1);
    }

    public int calculateCycleHelper(int n, int cycleNum) {
        if (n == 1) {
            return cycleNum;
        } else {
            return calculateCycleHelper(next(n), cycleNum + 1);
        }
    }

    public int next(int n) {
        if (n % 2 == 0) {
            n = n/2;
        }
        else {
            n = 3*n+1;
        }
        return n;
    }
}

Please overlook the efficiency and other related issues of the code, since that is not the issue here.

Coder47
  • 101
  • 1
  • 8

1 Answers1

3

It's because if (a < b) says the opposite of what you want. You need to swap the elements if b < a, because in the for loop

for (int i = a; i <= b; i++)

you want a to be less than b.

EDIT

For your updated question, the answer is that it's because the judge is checking the output. If you swap a and b the output from the line

 System.out.println (a + " " + b + " " + cycleMax);

changes, so the judge does not accept it.

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • And with that, it's established: I'm an idiot. Thank you so much! – Coder47 Nov 30 '15 at 23:37
  • @Coder47 No, we've all done things like that! Don't forget you can accept the answer (check the tick next to it) if it helped. – Paul Boddington Nov 30 '15 at 23:38
  • Actually, no. I take that back. I incorrectly wrote out my code above: even when it is if (b < a), the judge says it's wrong, even though on the test cases, it outputs correctly. – Coder47 Nov 30 '15 at 23:44
  • @Coder47 Your question doesn't make sense now. Both methods are the same in this version. I suggest you ask a new question. – Paul Boddington Nov 30 '15 at 23:49
  • I understand. That's exactly why I'm frustrated with this. Both versions do the same thing (if you comment out swap method A, and uncomment swap method B), the judge will accept it, but if you do the opposite, the judge will not. It still matches the original question: "Simply changing from Swap Method A to Swap Method B, the UVa judge goes from 'Wrong Answer' to 'Accepted'. Why is that?". Feel free to see for yourself: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=36&mosmsg=Submission+received+with+ID+16517896 – Coder47 Nov 30 '15 at 23:55
  • Well if you swap `a` and `b` it affects the output of the `println`. – Paul Boddington Nov 30 '15 at 23:58
  • I have no idea how the judge works. Does it check that you've printed the right thing in the line `System.out.println (a + " " + b + " " + cycleMax);`? If so, that would explain it. – Paul Boddington Dec 01 '15 at 00:01
  • It just checks the output. In both cases, I leave the System.output.println line the same. The only thing the switching affects is the for loop. (P.S., it's really annoying that I can't private message until I reach rank 15. I apologize in advance to everyone reading this if this comments section is super long!) – Coder47 Dec 01 '15 at 00:03
  • Well if you switch `a` and `b` the output from the line `System.out.println (a + " " + b + " " + cycleMax);` changes. Why is that not the answer to your question? – Paul Boddington Dec 01 '15 at 00:04
  • 1
    That's it! You got it! I'm swapping the a and b in switch method A, and what that means it that I am printing the order swapped, where as in the other case, I leave it untouched. The latter is exactly what the judge wants, which is why this is incorrect. Thank you! – Coder47 Dec 01 '15 at 00:07
  • No problem. Glad I could help. – Paul Boddington Dec 01 '15 at 00:07