A38507. 角谷猜想问题描述角谷猜想是正整数 N 如果为偶数则将其变换为二分之一,如果为奇数则将其变换 3 倍加 1,重复该过程将最终变为 4、2、1 的重复。到目前为止,该猜想尚未在数学上得到证明。编写程序输出指定范围内变化步骤最多的数及其变化步骤总数和变化过程。对于 13 其变化步骤为:13, 40, 20, 10, 5, 16, 8, 4, 2, 1。 根据上述描述,编写程序。在样例输出中,18 表示…
填空题
困难
知识点
题目描述
角谷猜想
问题描述
角谷猜想是正整数 N 如果为偶数则将其变换为二分之一,如果为奇数则将其变换 3 倍加 1,重复该过程将最终变为 4、2、1 的重复。到目前为止,该猜想尚未在数学上得到证明。编写程序输出指定范围内变化步骤最多的数及其变化步骤总数和变化过程。对于 13 其变化步骤为:13, 40, 20, 10, 5, 16, 8, 4, 2, 1。
根据上述描述,编写程序。在样例输出中,18 表示界于 10 和 20 之间的 18 变化步骤最多,共计变换 21 步,其变换过程如最后行所示。
变化步骤部分的箭头由英文字符减号和大于号联合构成,每个数之后均有。
样例输入
10,20
样例输出
18
21
18->9->28->14->7->22->11->34->17->52->26->13->40->20->10->5->16->8->4->2->1->
参考答案
First = int(input())
Second = int(input())
if First > Second:
First, Second = Second, First
maxStep = 0
maxNum = First
for i in range(First,Second+1):
N = i
stepCount = 1
while N != 1:
stepCount += 1
if N % 2 == 0:
N = N // 2
else:
N = N * 3 + 1
if stepCount > maxStep:
maxStep = stepCount
maxNum = i
print(maxNum)
print(maxStep)
上一题
下一题