cloud-security-wiki/docs/second/README.md

51 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### 1001 害死人不偿命的(3n+1)猜想 (15分)
**卡拉兹(Callatz)猜想**
对任何一个自然数n如果它是偶数那么把它砍掉一半如果它是奇数那么把(3n+1)砍掉一半。这样一直反复砍下去最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家大会上公布了这个猜想传说当时耶鲁大学师生齐动员拼命想证明这个貌似很傻很天真的命题结果闹得学生们无心学业一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……
我们今天的题目不是证明卡拉兹猜想而是对给定的任一不超过1000的正整数n简单地数一下需要多少步(砍几下)才能得到n=1
**输入格式**
每个测试输入包含1个测试用例即给出自然数n的值。
**输出格式**
输出从 n 计算到1需要的步数。
**输入样例**:
~~~ bash
3
~~~
**输出样例**:
~~~ bash
5
~~~
代码实现:
~~~ 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();
int steps =0;
while (N !=1) {
if (N %2 ==0) {
N = N /2;
} else {
N = (3 * N +1) / 2;
}
steps++;
}
System.out.println(steps);
}
}
~~~