3n+1问题

#include <iostream>

using namespace std;

int getLength(int value)
{
	if(value == 1)
		return 1;
	return value & 0x01 ? 1 + getLength(3 * value + 1) : 1 + getLength(value >> 1);
}

int getLongest(int start, int last)
{
	int maxlen = 0;
	for(int i = start; i <= last; i++)
	{
		int tempLen = getLength(i);
		maxlen = maxlen > tempLen ? maxlen : tempLen;
	}

	return maxlen;
}

int main()
{
	int start, last;
	int result;
	
	while(cin >> start >> last)
	{
		result = getLongest(start, last);
		cout << start << " " << last << " " << result << endl;
	}

	return 0;
}

你可能感兴趣的:(3n+1问题)