思路比较简单,给定任意一个正整数,when n is even,then n=n/2. when n is odd,then n=(3n+1).知道输出n=1,计算求出n输出为1所需要的步数。
这里只需要n大于0小于1000.
我的代码是这样的吗,使用的是Python3
n=int(input())
count=0
if n>0 and n<=1000:
while n!=1:
if n%2==0:
n=n/2
count=count+1
else:
n=(3*n+1)/2
count=count+1
print(count)