首页 > 其他 > 详细

Hdu oj 1032 The 3n + 1 problem

时间:2015-08-17 17:22:20      阅读:115      评论:0      收藏:0      [点我收藏+]

题目:点击打开链接

分析:注意细节。

#include<stdio.h>
int a1[1000010];
int calcue(int x)
{
	int len=1;
    while(x!=1)
    {
    	if(x%2==0)
    	{
    		x=x/2;
    		len+=1;
    	}
    	if(x==1)
    	return len;
    	if(x%2==1)
    	{
    		x=3*x+1;
    		len+=1;
    	}
    	if(x==1)
    	return len;
    }
    return len;
}
int main()
{
	int m,n;
	while(~scanf("%d%d",&m,&n))
	{
		int i;
		int t;
		int num=-10; 
		printf("%d %d ",m,n);//注意输出的顺序 
		if(m>n)//注意M N的大小关系 
		{
			t=m;
			m=n;
			n=t;
		}
		for(i=m;i<=n;i++)
		{
			a1[i]=calcue(i);
			if(num<a1[i])
			num=a1[i];
		}
		printf("%d\n",num);
	}
	return 0;
}


 

版权声明:本文为博主原创文章,未经博主允许不得转载。

Hdu oj 1032 The 3n + 1 problem

原文:http://blog.csdn.net/zm_11/article/details/47726929

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!