-3

My initial question was a bit vague, so here's the edited question.

The 3n+1 uVa problem is based on the Collatz Conjecture. Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such repeated operations, you will get 1. This has been proven for very large numbers by supercomputers.

UVa Online Judge is an online automated judge for programming problems hosted by University of Valladolid.

Here's a link to the problem statement: uVa 3n+1

Please read the problem statement before looking at my code! The uVa online judge runs your code against certain test-cases and tells you if your solution was right or wrong. It does not tell you why it failed (if your solution was wrong).

Here is the link again if you missed the first one: The link to the problem description

I don't understand what I am missing out or skipping (since I am getting Wrong Answer) in my code logic. I have tried lots of test cases, and they seem to be working fine. I know the logic can be compressed to a large extent, but I just need to figure out where the flaw is for now.

Here is my code. I know bits/stdc++ shouldn't be used, but currently, it's not affecting my code. Please review it if you can and help me out.

#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;

int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;

//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;

if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}

if(i==j)
{
    equalnums=true;
}

tempi=i; tempj=j;   

//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
    maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
    {
        if(num%2==1)
        {
        num = (3*num)+1;
        cycles++;
        }

        else
        {
        num=(num/2);
        cycles++;
        }
    }

    if(equalnums==true)
        {
            maxcycles=cycles;
            equalnums=false;
        }
//Resetting num
    num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}

Here is the output I am getting for the following input:

Input:

1 1

10 1

210 201

113383 113383

999999 1

Output:

1 1 1

10 1 20

210 201 89

113383 113383 248

999999 1 525

Another testcase:

Input:

1 5

10 8

210 202

113383 113383

999999 999989

Output:

1 5 8

10 8 20

210 202 89

113383 113383 248

999999 999989 259

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Saunved Mutalik
  • 381
  • 2
  • 19
  • 1
    You are missing or skipping a proper problem description. What's `uVa`? What's `WA`? What are you trying to do? What doesn't work? – Eugene Sh. Aug 29 '16 at 19:34
  • Hello. I have given a link to the problem description (it was too long to copy-paste here).[Problem Statement](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36) – Saunved Mutalik Aug 29 '16 at 19:36
  • What does your program output if the input is `1 1`? – NathanOliver Aug 29 '16 at 19:39
  • @SaunvedMutalik SO does not want links or copy-pasting, but *your* description. – deviantfan Aug 29 '16 at 19:40
  • 1
    Btw., `#include` is bad. – deviantfan Aug 29 '16 at 19:40
  • I edited my question. Please take a look now. – Saunved Mutalik Aug 29 '16 at 19:41
  • @NathanOliver See my inputs and outputs now. – Saunved Mutalik Aug 29 '16 at 19:43
  • Does it tell you what input you output fails on? – NathanOliver Aug 29 '16 at 19:44
  • @NathanOliver No it doesn't. Also, 0 is not a valid input, so there is no special code needed for that. – Saunved Mutalik Aug 29 '16 at 19:45
  • A valid and clear problem statement has been provided in the link (unless someone expects me to copy 200 lines of content here). There is no compilation error, the online judge does not tell me what output my code fails on. As far as I am concerned, this is the shortest code I can write at the moment. Thanks for putting it on hold. – Saunved Mutalik Aug 29 '16 at 19:46
  • `unless someone expects me to copy 200 lines of content here` I told you already, we don't expect that. But don't expect everyone to read this 200 lines to help you and/or do debugging work for you. If this means there is no way to ask, then ... well. – deviantfan Aug 29 '16 at 20:56
  • I've been trying to see what's wrong for almost a whole day now. I'm sure it's something very stupid. I tried explaining the statement here. I saw other people's questions (who had gotten 3n+1 Wrong Answers), who had much worse explanations than mine. I thought I'd get help on StackOverflow. – Saunved Mutalik Aug 30 '16 at 13:13

1 Answers1

0

The cycle length of the last number checked is never compared against maximum.

For Input:

8 9
9 9

The program outputs

8 9 4
9 9 20

But the correct answer is

8 9 20
9 9 20

Put

if (cycles > maxcycles) {
    maxcycles = cycles;
}

at the end of the for-loop, instead of at the beginning.

Daniel Rodriguez
  • 607
  • 6
  • 11
  • I'm pretty sure I've made this mistake in other programs too! Finally found the fix. Thank you! And the code got accepted now :) – Saunved Mutalik Sep 04 '16 at 18:44