Thursday, May 18, 2017

UVa online judge 694

UVa online judge
problem:694
( The Collatz sequence)


problem: 





solution:


#include<bits/stdc++.h>
using namespace std;
long long int c;
void rec(long long int a, long long int b)
{

    if(a>b) return;
    c++;
    if(a==1) return;
    else
    {
        if(a%2==0) rec(a/2,b);
        else rec(3*a+1,b);
    }
}
int main()
{

    long long int A,B,i,a;
    for(i=1;;++i)
    {
        scanf("%lld %lld",&A,&B);

        if(A<0&&B<0) break;
        else
        {
            c=0;
            rec(A,B);
        }
        printf("Case %lld: A = %lld, limit = %lld, number of terms = %lld\n",i,A,B,c);
    }
    return 0;
}

No comments:

Post a Comment