|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我写的作业总跟甲鱼老师有很大区别呢 又来麻烦大家看看15回的动手01题 角谷猜想 这样写可以不
numbera = int(input())
while numbera != 1:
if numbera % 2 == 0:
print(numbera, "/ 2 =", numbera/2 )
numbera = numbera / 2
else:
print(numbera, "* 3 + 1 =", numbera * 3 + 1)
numbera = numbera * 3 + 1
试了两个数 好像运行都没问题呢
99
99 * 3 + 1 = 298
298 / 2 = 149.0
149.0 * 3 + 1 = 448.0
448.0 / 2 = 224.0
224.0 / 2 = 112.0
112.0 / 2 = 56.0
56.0 / 2 = 28.0
28.0 / 2 = 14.0
14.0 / 2 = 7.0
7.0 * 3 + 1 = 22.0
22.0 / 2 = 11.0
11.0 * 3 + 1 = 34.0
34.0 / 2 = 17.0
17.0 * 3 + 1 = 52.0
52.0 / 2 = 26.0
26.0 / 2 = 13.0
13.0 * 3 + 1 = 40.0
40.0 / 2 = 20.0
20.0 / 2 = 10.0
10.0 / 2 = 5.0
5.0 * 3 + 1 = 16.0
16.0 / 2 = 8.0
8.0 / 2 = 4.0
4.0 / 2 = 2.0
2.0 / 2 = 1.0
50 / 2 = 25.0
25.0 * 3 + 1 = 76.0
76.0 / 2 = 38.0
38.0 / 2 = 19.0
19.0 * 3 + 1 = 58.0
58.0 / 2 = 29.0
29.0 * 3 + 1 = 88.0
88.0 / 2 = 44.0
44.0 / 2 = 22.0
22.0 / 2 = 11.0
11.0 * 3 + 1 = 34.0
34.0 / 2 = 17.0
17.0 * 3 + 1 = 52.0
52.0 / 2 = 26.0
26.0 / 2 = 13.0
13.0 * 3 + 1 = 40.0
40.0 / 2 = 20.0
20.0 / 2 = 10.0
10.0 / 2 = 5.0
5.0 * 3 + 1 = 16.0
16.0 / 2 = 8.0
8.0 / 2 = 4.0
4.0 / 2 = 2.0
2.0 / 2 = 1.0
你的程序实现了角谷猜想,输出结果也是正确的。但是在 Python 中,建议使用整数除法运算符 // 来代替 / 进行整数除法,避免产生不必要的浮点数。你可以稍微修改一下程序,如下所示:
numbera = int(input())
while numbera != 1:
if numbera % 2 == 0:
print(numbera, "// 2 =", numbera // 2)
numbera = numbera // 2
else:
print(numbera, "* 3 + 1 =", numbera * 3 + 1)
numbera = numbera * 3 + 1
这样,你的程序输出就会是整数,更加易读。
例如,输入 99 的输出结果:
99 * 3 + 1 = 298
298 // 2 = 149
149 * 3 + 1 = 448
448 // 2 = 224
...
2 // 2 = 1
总的来说,你的程序实现了角谷猜想,只需要稍作修改即可。
|
|