Accepted solution

UVA 100 The 3n + 1 problem Solution in C++

Open original problem
Source code

#include <iostream>

using namespace std;

int main()

{

    int i,j,count=1;

    int total =0;

    while(cin>>i && cin>>j)

    {

        total=0;

        cout<<i<<" "<<j<<" ";

        if(i>j)

        {

            swap(i,j);

        }

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

        {

            int d = a;

            count=1;

            while(d != 1)

            {

                if(d%2==0)

                {

                    d = d/2;

                }

                else

                {

                    d = (d*3) + 1;

                }

                count++;

            }

            if(total<=count)

            {

               total=count;

            }

        }

        cout<<total<<endl;

    }

    return 0;

}
Join the discussion

Add your comment