1038: 角谷猜想

来源:互联网 发布:sql 存储过程中 事物 编辑:程序博客网 时间:2024/05/17 22:26

题目

Description
角谷猜想:
日本一位中学生发现一个奇妙的“定理”,请角谷教授证明,而教授无能为力,于是产生角谷猜想。猜想的内容是:任给一个自然数,若为偶数除以2,若为奇数则乘3加1,得到一个新的自然数后按照上面的法则继续演算,若干次后得到的结果必然为1。请编程验证。
Input
任一正整数
Output
演算的过程
Sample Input
10
Sample Output
10/2=5
5*3+1=16
16/2=8
8/2=4
4/2=2
2/2=1


代码块

//输入包import java.util.Scanner;public class Main {    public static void main(String[] args) {        Scanner cin = new Scanner(System.in);//输入流        int n = cin.nextInt();        while (n != 1) {//while判断            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;            }        }    }}
0 0
原创粉丝点击