3n+1 - Wrong Answer - UVA Online Judge

Hello everyone,

Could anyone please explain why I keep getting the "Wrong Answer" verdict?

I don't read the input from a file (only standard console), I don't put endl or \n at the end of the input and yet the online judge doesn't accept it. Needless to say that when I compile and test it in Xcode, it works well and passes all the input/output tests. Please, help. I think of myself as a beginner (well, I studied coding 15 years ago and now want to get back on track) and stumbling at the beginning really discourages me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
//  main.cpp
//  The 3n+1 Problem
//
//  http://uva.onlinejudge.org/external/1/100.pdf



#include <iostream>
using namespace std;

// calculating the cycle length
int three_N_Plus_One (int n)
{
  
    int i=1;
    int j=n;
    while (j!=1)
    {
        if ((j%2)!=0) { j=3*j+1; }
        else { j = j/2; }
        i++;
    }
    return i;
}

// calculating the maximum cycle length
int funcMax (int i, int j)
{
    int max=three_N_Plus_One (i), current=0;
    for (int index=i+1; index<=j; index++)
    {
        current=three_N_Plus_One(index);
        if (current>max) { max=current; }
    }
    return max;
}


int main(int argc, const char * argv[])
{
    // initializing and inputting i and j
    int i=0, j=0;
    cin >> i >> j;
    if (i>j) { int swap=i; i=j; j=swap; }
    
    // printing the output
     cout << i << " " << j << " " << funcMax(i,j);
    
    return 0;
}
Last edited on
You take input only once, when you need to take all inpit and process all pairs. On the page you see example of input:
1 10
100 200
201 210
900 1000
Your program will read only 1 10 discarding the rest.
Yes, after posting this topic I continued googling and was more successful.

I'll recap what I have found for this problem:

1) the input should get an input pair until told otherwise, for example, by entering a character.

2) to be on the safe side I've replaced int with long long int.

3) one should check if the first number is less than the second one, in other words, if i<j. If on the contrary, one should swap them and proceed. However, the output should be done in the initial input order (so, if the user inputs 10 1, the maximum length should be done for the numbers from 1 to 10, but the output line should look like 10 1 20).

I hope that will be of much help to those who're struggling with this problem.
Last edited on
Topic archived. No new replies allowed.