Monday, April 03, 2017

UVA:100(3N+1)

                    UVA ONLINE JUDGE
                       Problem:100(3N+1)



Problem link:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=36

Solution:


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

int main()
{

    int i,j;

    while ( scanf ("%d %d", &i, &j) != EOF )
    {

        int temp_i = i;
        int temp_j = j;

        if ( i > j ) swap (i, j);

        int max_cycle_length = 0;
        int cycle_length;

        while ( i <= j )
        {
            unsigned int n = i;
            cycle_length = 1;

            while ( n != 1 )
            {
                if ( n % 2 == 1 ) n = 3 * n + 1;
                else n /= 2;
                cycle_length++;
            }

            if ( cycle_length > max_cycle_length )
                max_cycle_length = cycle_length;

            i++;
        }

        printf ("%d %d %d\n", temp_i, temp_j, max_cycle_length);
    }

    return 0;
}

No comments:

Post a Comment