View Code of Problem 3866

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

typedef long long ll;
const int maxn = 1e5+50;
const ll INF = 0xffffffff;

int main()
{
    int n;
    scanf("%d",&n);
    int x = n;
    int y = 0;
    int f = 1;
    while(x>=10 || f)
    {
        if(f)
        {
            f = 0;
            if(x==0)
            {
                x+=(y&1)?-1:1;
            }
            else
            {
                if(x & 1)
                {
                    x = 3*x+1;
                }
                else
                    x/=2;
            }
        }
        else
        {
            x%=10;
            if(x==0)
            {
                x+=(y&1)?-1:1;
            }
            else
            {
                if(x & 1)
                {
                    x = 3*x+1;
                }
                else
                    x/=2;
            }
        }
    }
    cout<<x<<endl;
}






Double click to view unformatted code.


Back to problem 3866