U E D R , A S I H C RSS

3N+1 Problem/곽세환

소감

ACM첫번째 문제
between i and j에서 i는 j보다 클수도 있음
이것땜에 고생했었음...

소스

~cpp 
// 100
#include <iostream>
using namespace std;

int cycle(int n);

void main()
{
	int i, j, great_length;
	while (cin >> i >> j)
	{
		int temp_i = i;
		int temp_j = j;

		if (temp_i > temp_j)
		{
			int temp;
			temp = temp_i;
			temp_i = temp_j;
			temp_j = temp;
		}


		great_length = cycle(temp_i);

		while (temp_i < temp_j)
		{
			temp_i++;
			int temp;
			if ((temp = cycle(temp_i)) > great_length)
				great_length = temp;
		}
		cout << i << " " << j << " " << great_length << endl;
	}
}

int cycle(int n)
{
	int cnt = 1;
	while (n != 1)
	{
		if (n % 2 == 1)
			n = 3 * n + 1;
		else
			n = n / 2;
		cnt++;
	}
	return cnt;
}

댓글ㄹㄹ

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2022-08-10 14:59:02
Processing time 0.0084 sec