hdu 1032 The 3n + 1 problem
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1032
暴力模拟水
题目大意:根据给出算法计算走了多少步。
题目分析:一开始想打个表,发现不大可能,看discuss里人家说测试数据很弱,暴力能过,就只好暴力了:-(
code:
#include<stdio.h>
int chuli(int k,int step)
{
if(k==1)return step;
if(k%2)return chuli(3*k+1,step+1);
else return chuli(k/2,step+1);
}
int main()
{
int i,j,temp,k,flag;
while(scanf("%d%d",&i,&j)!=EOF)
{
flag=0;
if(i<j)
for(k=i;k<=j;k++)
{
temp=chuli(k,0);
if(flag<temp)flag=temp;
}
else
for(k=j;k<=i;k++)
{
temp=chuli(k,0);
if(flag<temp)flag=temp;
}
printf("%d %d %d\n",i,j,flag+1);
}
return 0;
}
PS:tricker还是有的,i、j不一定谁大谁小,而最后输出的时候,需要按i、j原序输出……