zoukankan      html  css  js  c++  java
  • java实现角谷步数

    你听说过角谷猜想吗?
    任意的正整数,比如 5, 我们从它开始,如下规则计算:
    如果是偶数,则除以2,如果是奇数,则乘以3再加1.
    如此循环,最终必会得到“1” !

    比如 5 的处理过程是:
    5
    16
    8
    4
    2
    1

    一个正整数经过多少步才能变成1, 称为角谷步数。
    对于5而言,步数也是5
    对于1,步数为0

    本题的要求是,从标准输入给定一个整数n(1<n<300)表示角谷步数
    求满足这个角谷步数的最小的正整数

    例如:

    输入:
    3
    则输出:
    8

    输入:
    4
    则输出:
    16

    输入:
    7
    则输出:
    3

    package com.liu.ex5;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
        public static ArrayList<Integer> list = new ArrayList<Integer>();
        
        public void getResult() {
            list.add(0);
            for(int i = 1;i <= 300000;i++) {
                int tempi = i;
                int count = 0;
                while(tempi > 1) {
                    if(tempi % 2 == 0) {
                        tempi = tempi / 2;
                        count++;
                    } else {
                        tempi = tempi * 3 + 1;
                        count++;
                    }
                }
                list.add(count);
            }
            return;
        }
        
        public void printResult(int n) {
            getResult();
            int result = list.indexOf(n);
            System.out.println(result);
            return;
        }
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            test.printResult(n);
        }
    }
    
  • 相关阅读:
    [CLYZ2017]day10
    标签
    FJOI2017一试滚粗
    [学习笔记]一些求gcd的方法的证明
    WC2017有感
    [学习笔记]splay
    [CLYZ2017]day9
    [CLYZ2017]day6
    转:Asp.net模板引擎技术(html)
    精华:ASP.NET开发网站程序安全解决方案(防注入等)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947964.html
Copyright © 2011-2022 走看看