View Code of Problem 3866

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <math.h>
#include <unordered_map>
#include <iterator>
#include <utility>
#include <functional>
using namespace std;
typedef long long ll;

ll f(int x, int y)
{
	ll ans = x;
	if (x % 2 == 0 && x)
	{
		ans = x / 2;
	}
	else if (x % 2 != 0)
	{
		ans = 3.0 * x + 1;
	}
	else if (y % 2 == 0)
	{
		ans = 1;
	}
	else
	{
		ans = -1;
	}
	return ans;
}

int main()
{
	int n;
	cin >> n;
	ll ans = n;
	int count = 0;
	do
	{
		if (count == 0)
		{
			ans = f(ans, count);
		}
		else
		{
			ans %= 10;
			ans = f(ans, count);
		}
		count++;
	} while (abs(ans) >= 10);
	cout << ans << endl;
	return 0;
}

Double click to view unformatted code.


Back to problem 3866