package com.example.demo; import java.util.ArrayList; import java.util.List; /** * * <P>Description: Joseph Problem</P> * @ClassName: YueSefu * @author Feng Hao on April 24, 2018 at 10:38:49 AM * @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); } // count from the Kth int k = 0 ; while (start.size() >0 ) { k = k + countNum; // The index position of the mth person k = k % (start.size()) - 1 ; System.out.println(String.format( "k %s size %s" , k,start.size())); // Determine whether the end of the queue is reached 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: recursive related code</P> * @ClassName: DiGui * @author Feng Hao on April 27, 2018 at 2:10:05 pm * * * Divide and conquer algorithm -- merge sort * / public class DiGui { public long[] array; /** * * <p>Title: threeJiaoHanShu</p> * <p>Description: trigonometric functions</p> * @author Feng Hao on April 27, 2018 at 2:11:52 pm * * Description of trigonometric functions * 1 3 6 10 15 21 28 * 1 2 3 4 5 6 7 * * Query the value of the nth item, and check the data law to know that the value of 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); // Explicit recursive base case if (n == 1 ) { return 1 ; }else { return n+three(n-1); } } /** * i! factorial * 2! 1*2 * 3! 1*2*3 * 4! 1*2*3*4 * <p>Title: jiecheng</p> * <p>Description: factorial</p> * @author Feng Hao April 27, 2018 at 2:37:26 pm */ @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: binary search method</p> * @author Feng Hao April 27, 2018 at 2:55:08 PM */ @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: Tower of Hanoi</p> * @author Feng Hao on April 27, 2018 at 3:52:21 PM * *Move the plate * A B C * 3 * first move n-1 in A to B * Then move the nth in A to C * Then move B in C to 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); } } }