UVA100-The 3n + 1 problem
- import java.util.*;
- class main{
- static int count = 1 ;
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
-
- while(sc.hasNext()){
- System.out.println(The3(sc.nextInt() , sc.nextInt() ));
- }
-
- }
- public static int The3(int Start , int End ){
- System.out.print(Start+" "+End+" ");
- int max = 0 ;
- if(Start > End){
- int tmp = Start ;
- Start = End ;
- End =tmp;
- }
- for(int s = Start ; s <= End ; s++){
- int value = The3(s);
- count = 1 ;
- if(value > max ) max = value ;
- }
- return max ;
- }
- public static int The3(int N){
- if(N == 1) return count ;
- count++ ;
- if(N%2 == 0){
- The3(N/2);
- }else{
- The3(3*N+1);
- }
- return count;
- }
-
-
-
-
-
- }
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。