zoukankan      html  css  js  c++  java
  • 算法学习一

    设有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));
        }
  • 相关阅读:
    JAVA多线程2 锁
    IE8标准模式下VML不能显示问题
    JAVA多线程1
    JAVA判断32位还是64位,调用不同的DLL
    JNA调用DLL
    如何提高执行力
    httpClient多线程请求
    【NodeJS】安装
    [转载]一个项目涉及到的50个Sql语句(整理版)
    resultMap中的collection集合出现只能读取一条数据的解决方法
  • 原文地址:https://www.cnblogs.com/cxxiao/p/12694300.html
Copyright © 2011-2022 走看看