您的位置:首页 > 其它

POJ 1207 && HDU 1032 The 3n + 1 problem(水~)

2015-06-21 10:17 197 查看
Description

根据给定的算法,可以计算一个整数的循环数,现在给定一个区间,计算这个区间的所有数的循环数,把最大的循环数输出

1. input n

2. print n

3. if n = 1 then STOP

4. if n is odd then n <– 3n+1

5. else n <– n/2

6. GOTO 2

Input

多组测试用例,每组用例两个数代表查询区间两个端点,以文件尾结束输入,所有数均大于0且小于10000

Output

对于每组用例,输出一行三个整数,前两个为查询区间端点,第三个为该区间最大的循环数

Sample Input

1 10

100 200

201 210

900 1000

Sample Output

1 10 20

100 200 125

201 210 89

900 1000 174

Solution

水题,暴力枚举区间每点即可,注意输入的两个点不一定按顺序输入,所以要先排个序

Code

#include<stdio.h>
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)

int main()
{
int s,e,i,j,max,count;
while(scanf("%d%d",&s,&e)==2)
{
max=0;
for(i=min(s,e);i<=max(s,e);i++)//区间两端点不一定按顺序输入
{
count=1;
j=i;
while(j!=1)
{
if(!(j%2))
j/=2;
else
j=3*j+1;
count++;//循环数
}
max=max(max,count);//更新值
}
printf("%d %d %d\n",s,e,max);//按格式输出
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: