1. 程式人生 > >POJ 1207 The 3n + 1 problem G++

POJ 1207 The 3n + 1 problem G++

 

 

#include <iostream>
using namespace std;
int hs[10001];
int main()
{
	for(int i=1;i<10001;i++)
	{
		int js=1;
		int t=i;
		while(t!=1)
		{
			js++;
			if(t%2==1)
			{
				t=3*t+1;
			}else
			{
				t=t/2;
			}
		}
		hs[i]=js;
	}
	while(1)
	{
		int a,b;
		cin>>a;
		if(cin.eof()==1)
		{
			break;
		}
		cin>>b;
		if(a<=b)//謝謝博友文章 
		{
			int maxt=0;
			for(int i=a;i<=b;i++)
			{
				maxt=max(maxt,hs[i]);
			}
			cout<<a<<" "<<b<<" "<<maxt<<endl;			
		}else
		{
			int maxt=0;
			for(int i=b;i<=a;i++)
			{
				maxt=max(maxt,hs[i]);
			}
			cout<<a<<" "<<b<<" "<<maxt<<endl;			
		}
	}
	return 0;
}