2017年8月12日 星期六

UVA100-The 3n + 1 problem

  1. import java.util.*;  
  2. class main{  
  3. static int count = 1 ;  
  4. public static void main(String[] args){  
  5. Scanner sc = new Scanner(System.in);  
  6.   
  7. while(sc.hasNext()){  
  8.  System.out.println(The3(sc.nextInt() , sc.nextInt() ));  
  9. }  
  10.   
  11. }  
  12. public static int The3(int Start , int End ){  
  13. System.out.print(Start+" "+End+" ");  
  14. int max = 0 ;  
  15. if(Start > End){  
  16. int tmp = Start ;  
  17.  Start = End ;  
  18.  End =tmp;  
  19.  }  
  20.   for(int s = Start ; s <= End ; s++){  
  21.      int value = The3(s);  
  22.      count = 1 ;  
  23.      if(value > max ) max = value ;  
  24.   }  
  25. return max ;  
  26. }  
  27. public static int The3(int N){  
  28. if(N == 1return count ;  
  29.    count++ ;  
  30.   if(N%2 == 0){  
  31.      The3(N/2);  
  32.   }else{  
  33.      The3(3*N+1);  
  34.   }  
  35. return count;  
  36. }  
  37.   
  38.   
  39.   
  40.   
  41.   
  42. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。