Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

奥运天庆祝!已经连做8天的题了!&&&为了减少时间 我加了个数组存已算过的值!

Posted by xunilyao at 2008-08-08 09:53:29
#include<iostream>
using namespace std;

int rem[10002]={0};

int fun(int n)
{
	int i=1;
	while(1)
	{
		if(n<=10000)
		{
			if(rem[n]!=0)
				return	(i+rem[n]-1);
		}

		if (n ==1) 
			return i;
		if(n%2!=0)
			n=3*n+1;
		else 
			n =n/2;
		i++;
	}
}

int main()
{
	//freopen("1207.txt","r",stdin);
	int first , end ;
	int i;
	int max = 0;
	int tem ;
	int flag=-1;
	while(cin>>first>>end)
	{
		flag=-1;
		max=0 ;
		if(first>end)
		{
			flag=first;
			first=end;
			end=flag;
		}

		for(i=first;i<=end;i++)
		{
			if(rem[i]==0)
			{
				tem=fun(i);	
				rem[i]=tem;
			}
			if(max<rem[i])
				max=rem[i];
		}

		if(flag!=-1)
		{
			flag=first;
			first=end;
			end=flag;		
		}
		printf("%d %d %d\n",first,end,max);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator