U E D R , A S I H C RSS

3n 1/이도현

2005-12-30 14:39:20 Accepted 3.256 436 56031 C++ 100 - The 3n + 1 problem

로봇 Accepted라는 감격.
다. 무나 꼼꼼 다.

1. 력 2개가 범로 들가는 데 단 라는 못되다. ( )
2. 대단다. (& ), 나기2(right shift 1) - 됨.
3. 고리 다. - 반드 다.
4. 기 문 - 단 망가는 모다.

~cpp
// The 3n + 1 problem
// UVa ID : 100
#include <iostream>
using namespace std;
int cycle_length(int input);

int main()
{
	int input1, input2;

	while (cin >> input1 >> input2)
	{
		int i;
		int max_count = -1;
		int temp = 0;
		
		//  대 뒤바뀌면 된다!!
		cout << input1 << " " << input2 << " ";

		//  력보다 더  (for문 러 방)
		if (input1 > input2)
		{
			int swap = input1;
			input1 = input2;
			input2 = swap;
		}

		for (i = input1; i <= input2; i++)
		{
			temp = cycle_length(i);				// cycle legnth 기
			if (temp > max_count)
				max_count = temp;				//  를 max_count로
		}
		cout << max_count << endl;
	}

	return 0;
}

// cycle length 구기
int cycle_length(int input)
{
	int argument = input;		//   
	int count = 0;				// 
	
	while (true)
	{
		// 
		if (argument == 1)
		{
			count++;
			break;
		}

		// LSB가 0, 1다.
		if ((argument & 1) == 0)
		{
			// 나기 2는 right shift를   는 것과 같다.
			argument >>= 1;
			count++;
		}
		else
		{
			// 라면 반드 다.
			argument = 3 * argument + 1;
			argument >>= 1;
			count += 2;
		}
	}

	return count;
}

덧글

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:17
Processing time 0.0093 sec