Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

3N 1Problem/허아영

From ZeroWiki
Revision as of 12:46, 27 March 2026 by Maintenance script (talk | contribs) (Table transclusion repair v1)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

소감

2006.1.6                8.842s

코드

#include <iostream>

using namespace std;

int count = 0;
int i, j;

bool input()
{
	count = 0;
	if (cin >> i >> j)
		return true;
	return false;
}

int process(int n)
{
	count++;
	if(n == 1) 
	{
		return count; 
	} else if(n % 2 == 1)
	{
		n = 3 * n + 1;
		return process(n);
	}
	else {	
		n /= 2;
		return process(n);
	}
}

int compare()
{
	int temp;
	int temp2;
	if(i == j)
		return process(i);
	else
	{
		temp = 0;
		count = 0;
		if(i > j)
		{
			for(int k = j; k <= i; k++)
			{
				temp2 = process(k);
				count = 0;
				if(temp <= temp2)
				{
					temp = temp2;
				}
			}
			
		} else
		{
			for(int k = i; k <= j; k++)
			{
				temp2 = process(k);
				count = 0;
				if(temp <= temp2)
				{
					temp = temp2;
				}
			}
		}
		return temp;
	}
}

int main() 
{
	while(input())
	{
		int temp= compare();
		cout << i << " " << j << " " << temp << endl;;
	}
	return 0;
}

이때 내가 어떻게 이 문제를 풀었을까.. 대단대단.. 하지만 고치고 싶은 부분이 많이 보인다 ㅋ - 나


3N+1Problem