NYOJ 271

题意:3*n+1问题,输出区间内的最大处理次数

#include<stdio.h>
int a[10001];
void fun()
{
	int temp,cnt;int i,k=0;
	for(i=1;i<=10001;i++)
		{
			temp=i;cnt=0;//cnt初始化必须在for内
			while(temp!=1)
			{
				if(temp&1)
					temp=3*temp+1;
				else
					temp>>=1;
				cnt++;
				//printf("%d\n",cnt);
			}
			a[k++]=cnt;//实际上k没必要,k相当于(i-1)		
		}
}
int main()
{
	int m,n,max;int i,j;
	fun();
	while(~scanf("%d%d",&m,&n))
	{
		max=0;
		for(i=m;i<=n;i++)
			if(max<a[i-1])
				max=a[i-1];			
		printf("%d %d %d\n",m,n,max+1);
	}
	return 0;
}

//超时

#include<stdio.h>
int main()
{
	int m,n;int i,j;int max,temp,cnt;
	while(~scanf("%d%d",&m,&n))
	{
		max=0;
		for(i=m;i<=n;i++)
		{
			temp=i;cnt=0;//cnt初始化必须在for内
			while(temp!=1)
			{
				if(temp&1)
					temp=3*temp+1;
				else
					temp>>=1;
				cnt++;
			//	printf("%d\n",cnt);
			}
			if(max<cnt)
			{
				max=cnt;
			//	printf("%d\n\n",max);
			}
		}
		printf("%d %d %d\n",m,n,max+1);
	}
	return 0;
}

  

posted @ 2012-06-03 22:51  加拿大小哥哥  阅读(160)  评论(0编辑  收藏  举报