hdu1032水

注意i可能比j大
#include <iostream>
#include <cstdio>
using namespace std;

int cal(int num)
{
	int count = 1;
	while(1)
	{
		if(num == 1)
		{
			return count;
			break;
		}
		else
		{
			if(num % 2 == 1)
			{
				num = num*3 + 1;
			}
			else
			{
				num = num/2;
			}
			count++;
		}
	}
	return count;
}

int main()
{
	int i,j;
	while(cin>>i>>j)
	{
                int a = i;
                int b = j;
                if(i > j)
                {
 
                    int tmp = i;
                    i = j;
                    j = tmp;
                }
		cout<<a<<" "<<b<<" ";
		int max = 0;
		for(int a = i; a <= j;a++)
		{
			int res = cal(a);
			if(max < res)
				max = res;
		}
		cout<<max<<endl;
	}
	return 0;
}

你可能感兴趣的:(hdu1032水)