zoukankan      html  css  js  c++  java
  • 数据结构和算法(三)-约瑟夫问题,递归,汉诺塔

    package com.example.demo;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 
     * <P>Description: 约瑟夫问题</P>
     * @ClassName: YueSefu
     * @author 冯浩  2018年4月24日 上午10:38:49
     * @see TODO
     */
    public class YueSefu {
        
           public static void yuesefu(int totalNum, int countNum) {  
                      // 初始化人数  
                      List<Integer> start = new ArrayList<Integer>();  
                      for (int i = 1; i <= totalNum; i++) {  
                          start.add(i);  
                      }  
                      //从第K个开始计数  
                      int k = 0;  
                      while (start.size() >0) {  
                          k = k + countNum;  
                          //第m人的索引位置  
                          k = k % (start.size()) - 1;  
                          System.out.println(String.format("k %s size %s", k,start.size()));
                         // 判断是否到队尾  
                          if (k < 0) {  
                              System.out.println(start.get(start.size()-1));  
                              start.remove(start.size() - 1);  
                              k = 0;  
                          } else {  
                              System.out.println(start.get(k));  
                              start.remove(k);  
                          }  
                      }  
                  }  
    
        public static void main(String[] args) {
            YueSefu.yuesefu(41, 3);
    //        int a=3%1;
    //        System.out.println(a);
        }
    
    }
    package com.example.demo;
    
    import org.junit.Test;
    
    /**
     * 
     * <P>Description: 递归相关代码</P>
     * @ClassName: DiGui
     * @author 冯浩  2018年4月27日 下午2:10:05
     * 
     * 
     * 分治算法--归并排序
     */
    public class DiGui {
        
        
        public long[] array;
        
        
        /**
         * 
         * <p>Title: threeJiaoHanShu</p>
         * <p>Description: 三角函数</p>
         * @author 冯浩  2018年4月27日 下午2:11:52
         * 
         * 三角函数描述
         * 1 3 6 10 15 21 28
         * 1 2 3 4  5  6  7
         * 
         * 查询第n项的值,查看数据规律可知n的值=n+n-1+n-2...1
         */
        @org.junit.Test
        public void threeJiaoHanShu() {
            int three = three(3);
            System.out.println(three);
        }
    
        public int three(int n) {
            System.out.println(n);
            //明确递归的基准情况
            if(n == 1) {
                return 1;
            }else {
                return n+three(n-1);
            }
        }
        
        
        /**
         * i!  阶乘
         * 2! 1*2
         * 3! 1*2*3
         * 4! 1*2*3*4
         * <p>Title: jiecheng</p>
         * <p>Description: 阶乘</p>
         * @author 冯浩  2018年4月27日 下午2:37:26
         */
        @org.junit.Test
        public void jiecheng() {
            int jie = jie(4);
            System.out.println(jie);
        }
        
        public int jie(int i) {
            if(i==1) {
                return 1;
            }else {
                return i * jie(i-1);
            }
        }
        
        
        /**
         * 
         * <p>Title: twoFind</p>
         * <p>Description: 二分查找法</p>
         * @author 冯浩  2018年4月27日 下午2:55:08
         */
        @org.junit.Test
        public void twoFind() {
            
            this.array= new long[] {1,2,4,6,8,9};
            long find = find(4, array.length, 0);
            System.out.println(find);
        }
        
        public long find(long key,int hight,int low) {
            int curIn=(low+hight)/2;
            
            if(array[curIn]==key) {
                return curIn;
            }else if(low>hight) {
                return array.length;
            }else {
                if(key<array[curIn]) {
                    return find(key,curIn-1,low);
                }else {
                    return find(key,hight,curIn+1);
                }
            }
        }
        
        
        /**
         * 
         * <p>Title: hannuota</p>
         * <p>Description: 汉诺塔</p>
         * @author 冯浩  2018年4月27日 下午3:52:21
         * 
         *移动盘子
         *     A    B    C
         *     3
         * 先将A中的n-1移动到B
         * 再将A中的第n个移动到C
         * 再将B中的移动到C    
         */
        @Test
        public void hannuota() {
            moveTower(3, "A", "B", "C");
        }
        
        public void moveTower(int n,String from,String inner,String to) {
            if(n == 1) {
                System.out.println("Disk 1 from "+from+" to "+ to);
            }else {
                moveTower(n-1, from, to, inner);
                System.out.println("Disk "+n+" from "+from+" to "+ to);
                moveTower(n-1, inner, from, to);
            }
        }
        
        
    }
  • 相关阅读:
    CSP-S2019 退役记
    近期考试反思
    有关近期情况的总结与反思
    我好难啊
    AFO
    智障错误集锦
    关于博客密码【asd
    关于csp-s的各种问题整理
    CSP-S 临别赠言( 二 )
    分层图最短路 乱搞分享
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/8965859.html
Copyright © 2011-2022 走看看