• 算法学习一


    设有n个人依围成一圈,从第1个人开始报数,数到第m个人出列,
    然后从出列的下一个人开始报数,数到第m个人又出列, …,
    如此反复到所有的人全部出列为止。设n个人的编号分别为 1, 2, …, n,打印出

    • 利用余数
    • 利用m-1这一关键数字
     1  public static void main(String[] args) {
     2  /*
     3     * 设有n个人依围成一圈,从第1个人开始报数,数到第m个人出列,
     4     * 然后从出列的下一个人开始报数,数到第m个人又出列,
     5      * …,如此反复到所有的人全部出列为止。
     6      * 设n个人的编号分别为 1, 2, …, n,打印出出
     7     * */
     8         List<Integer> list = new GetRemainder().getList(30, 5);
     9         System.out.println(list);
    10     }
    11 
    12     private List<Integer> getList(int lenth, int count) {
    13         List<Integer> source = new ArrayList();
    14         List<Integer> out = new ArrayList();
    15         for (int i = 1; i <= lenth; i++) {
    16             source.add(i);
    17         }
    18         int index = 0;
    19         while (source.size() > 0) {
    20             //4 8 12 16 20 24
    21             System.out.println("index : "+(index + count - 1)+" % "+source.size()+" = "+((index + count - 1) % source.size()));
    22             index = (index + count - 1) % source.size();
    23             out.add(source.get(index));
    24             source.remove(index);
    25 
    26         }
    27         return out;
    28     }

     java 写一个方法1000 的阶乘

    public static void main(String[] args) {
            long t = System.currentTimeMillis();
            System.out.println(factorial(new BigInteger("1000")));
            System.out.println(System.currentTimeMillis() - t);
            t = System.currentTimeMillis();
            System.out.println(factorial2(new BigInteger("1000"), BigInteger.ONE));
            System.out.println(System.currentTimeMillis() - t);
        }
    
        /**
         * 使用线性递归计算阶乘
         *
         * @param n
         * @return
         */
        public static BigInteger factorial(BigInteger n) {
            if (n.compareTo(BigInteger.ZERO) < 0) return BigInteger.ZERO;
    
            if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
                return new BigInteger("1");
            }
            return n.multiply(factorial(n.subtract(BigInteger.ONE)));
        }
    
        /**
         * 使用尾递归计算阶乘
         * 如果一个函数中所有递归形式的调用都出现在函数的末尾,我们称这个递归函数是尾递归的。
         * 当递归调用是整个函数体中最后执行的语句且它的返回值不属于表达式的一部分时,这个递归调用就是尾递归。
         * 尾递归函数的特点是在回归过程中不用做任何操作,这个特性很重要,因为大多数现代的编译器会利用这种特点自动生成优化的代码。
         * 尾递归是极其重要的,不用尾递归,函数的堆栈耗用难以估量,需要保存很多中间函数的堆栈。
         * 通过参数传递结果,达到不压栈的目的
         *
         * @param n
         * @param result
         * @return
         */
        public static BigInteger factorial2(BigInteger n, BigInteger result) {
            if (n.compareTo(BigInteger.ZERO) < 0) return BigInteger.ZERO;
    
            if (n.equals(BigInteger.ONE) || n.equals(BigInteger.ZERO)) {
                return result;
            }
    
            return factorial2(n.subtract(BigInteger.ONE), n.multiply(result));
        }
  • 相关阅读:
    basic use of sidekiq
    查看远程git log
    通过rails console执行sql语句
    通过rails打开数据库
    如何用rake tasks 生成migration对应的sql
    Remainders Game (中国剩余定理)
    binary-tree-preorder-traversal
    minimum-depth-of-binary-tree (搜索)
    Find a multiple POJ
    linked-list-cycle (快慢指针判断是否有环)
  • 原文地址:https://www.cnblogs.com/cxxiao/p/12694300.html
Copyright © 2020-2023  润新知