Java - Recursive thinking

/ ** 
 * factorial simple implementation 
 * @param n- 
 * @return 
 * / 
public  static  Double getFactorial ( Double n-) {
     for ( Double I = n--. 1; I> 0; i-- ) { 
        n- * = I; 
    } 
    return n-; 
} 

/ ** 
 * factorial 
 * = n-n-* (. 1-n-) * (n-2-). 1 * ... *! 
 * @param n- 
 * @return 
 * / 
public  static  int getFactorialValue ( int n-) {
     IF (n-==. 1 ) {
        return . 1 ; 
    } the else {
         return getFactorialValue (n--1) * n-; 
    } 
} 

/ ** 
 * with recursive Fibonacci number, suitable for solving the relatively small position value 
 * 01123581321. .. 
 * @param n- 
 * @return 
 * / 
public  static  int getFibonacciValue ( int n-) {
     IF (n-<= 0) return 0 ;
     IF (n-<= 2 ) {
         return . 1 ; 
    } the else {
         returngetFibonacciValue (. 1-n-) getFibonacciValue + (2-n- ); 
    } 
} 

/ ** 
 * List all directories and files in a directory 
 * @param path 
 * @return 
 * / 
public  static  void GetDir (String path) throws Exception { 
    File File = new new File (path);
     IF (file.isDirectory ()) { 
        System.out.println ( "the Dir" + file.getPath ()); 
        File [] fileArr = File.listFiles ();
         for (File F: fileArr) { 
            GetDir (f.getPath ()); 
        } 
    }the else  IF (file.isFile ()) { 
        System.out.println ( "File" + file.getPath ()); 
    } the else {
         the throw  new new Exception (file.getPath () + "?! non Dir non File" ); 
    } 
} 

 / ** 
 * HANOR 
 * FUNC: 
 ! n-IF * = 0 the then; predetermined value 
 * func (n-1, a , c, b); the n-1 is moved to a plate of a b, c to an auxiliary column (note that the order of parameters) 
 * move a [n-] to C; mobile last dish on a to C 
 * FUNC (n-1, b, a, C); a n-1 a plate is moved by b to c, to the auxiliary column a 
 * endif; complete 
 * @param n- 
 * @param a 
 * @param B 
 *@param C
  * / 
public  static  void getHanoi ( int n-, String A, B String, String C) {
     IF (n-==. 1 ) { 
        System.out.println ( "moving plate" + n + "from" + a + "to" + C); 
    } the else { 
        getHanoi (n- -1 , A, C, B); 
        System.out.println ( "moving plate" + n + "from" + a + "to" + C); 
        getHanoi (n- -1 , B, A, C); 
    } 
} 

/ ** 
 * dichotomy lookup value: The principle is to find the middle value 
 * must be sorted list,Ascending Descending can 
 * 
 * @param  array ordered array,But not limited to an array
 * @param Start Find array index 
 * @param End ending array index lookup 
 * @param value searchValue to search for 
 * @return 
 * / 
public  static  int Search ( int [] Array, int Start, int End, int searchValue) {
     IF (! Array = null && be array.length> 0 ) {
         int Middle = (Start + End) / 2 ;
         int middleValue = Array [Middle];
         IF (searchValue == middleValue) {
             return Middle;
        } The else IF (searchValue < middleValue) {
             // query value is less than the value, the value in the previous search again, narrow 
            return Search (Array, Start, Middle-. 1 , searchValue); 
        } the else {
             // query value is greater than the value in the the value search again later, narrow 
            return Search (Array, middle +. 1 , End, searchValue); 
        } 
    } the else {
         return -1 ; 
    } 
}

 

Guess you like

Origin www.cnblogs.com/zhoux955792/p/11518446.html