酷代码 AI
菜单

976年的一天,《华盛顿邮报》于头版头条报道了一条数学新闻。文中记叙了这样一个故事: 70年代中期,美国各所大学校园内,人们都像发疯一般,夜以继日,废寝忘食地玩弄一种数学游戏。这个游戏十分简单:任意写出一个正整数N,并且按照以下的规律进行变换:     如果是个奇数,则下一步变成3N+1。     如果是个偶数,则下一步变成N/2。 不单单是学生,甚至连教师、研究员、教授与学究都纷纷加入。为什么这种游戏的魅力经久不衰?因为人们发现,无论N是怎样一个数字,最终都无法逃脱回到谷底1。准确地说,是无法逃出落入底部的4-2-1循环,永远也逃不出这样的宿命。 这就是著名的“冰雹猜想”。(又称角谷猜想,因为是一个名叫角谷的日本人把它传到中国) 本题需要你来验证冰雹猜想的计算过程:给定一个整数N,将N的变换过程打印出来。 输入 任一正整数 𝑛 [ 2 , 10000 ] 输出 输出演算的过程. 样例输入 复制 10 样例输出 复制 10/2=5 5*3+1=16 16/2=8 8/2=4 4/2=2 2/2=1

Python ```python n = int(input()) while n != 1: if n % 2 == 0: print(f"{n}/2={n // 2}") n //= 2 else: print(f"{n}*3+1={n * 3 + 1}") n = n * 3 + 1 ``` Pascal ```pascal program Hailstone; var n: longint; begin readln(n); while n <> 1 do begin if n mod 2 = 0 then begin writeln(n, '/2=', n div 2); n := n div 2; end else begin writeln(n, '*3+1=', n * 3 + 1); n := n * 3 + 1; end; end; end. ``` C++ ```cpp #include <iostream> using namespace std; int main() { int n; cin >> n; while (n != 1) { if (n % 2 == 0) { cout << n << "/2=" << n / 2 << endl; n /= 2; } else { cout << n << "*3+1=" << n * 3 + 1 << endl; n = n * 3 + 1; } } return 0; } ``` C ```c #include <stdio.h> int main() { int n; scanf("%d", &n); while (n != 1) { if (n % 2 == 0) { printf("%d/2=%d\n", n, n / 2); n /= 2; } else { printf("%d*3+1=%d\n", n, n * 3 + 1); n = n * 3 + 1; } } return 0; } ``` Java ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); while (n != 1) { if (n % 2 == 0) { System.out.println(n + "/2=" + (n / 2)); n /= 2; } else { System.out.println(n + "*3+1=" + (n * 3 + 1)); n = n * 3 + 1; } } } } ``` [2025-12-27 19:49:58 | AI问答 | 562点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多