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.
//
// main.cpp
// The 3n+1 Problem
//
// http://uva.onlinejudge.org/external/1/100.pdf
#include <iostream>
usingnamespace 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, constchar * 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;
}
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.