Recursive algorithm:

 (1). Find: the value of 1*2*3*4*5.......*n

  1. package  suanfa;  
  2. /** 
  3.  * Created by tl on 2016/4/10. 
  4.  */  
  5. public class  Digui {   
  6.     public static int  digui ( int  n) {    
  7.         if(n==1||n==0){  
  8.             return n;  
  9.         }else{  
  10.             System.out.println( "Execute "  + n +  " times" );  
  11.             return  n * digui (n- 1 );  
  12.         }  
  13.     }  
  14. publicstaticvoid main (String[] args){    
  15.     System.out.print(digui(n));  

(2). Find the value of 1+2+3+4+5...+n

  1.     staticint  count(int n){   
  2.         if(n>0){  
  3.             return n+count(n-1);  
  4.         }else{  
  5.             return0;   
  6.         }  
  7.   
  8.     }  
  9.   
  10.     publicstaticvoid main(String args[])    
  11.     {  
  12.         int sum=count(n);  
  13.         System.out.println(sum);  
  14.     }  

(3). Find the value of 1,1,2,3,5,8,13,21,34 .....n bits

  1. staticint count(int n){   
  2.     if(n==1||n==2) {  
  3.         return1;   
  4.     }  
  5.      return count(n-1)+count(n-2);  
  6. }  
  7.   
  8. publicstaticvoid main(String args[])    
  9. {  
  10.     int sum=count(n);  
  11.     System.out.println(sum);  


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326134534&siteId=291194637