100 - The 3n + 1 problem UVA Online Judge Solution

7:24 PM | , , , ,



import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        while(s.hasNextInt()){
            int bil1=s.nextInt();
            int bil2=s.nextInt();
            int max=1;
            int a,b;
            if(bil1>bil2){
                a=bil2;
                b=bil1;
            }else{
                a=bil1;
                b=bil2;
            }
            for (int i = a; i <= b; i++) {
                int n=i,jml=1;
                while(n!=1){
                    if(n%2!=0) n=3*n+1;
                    else n=n/2;
                    jml++;
                }
                if(jml>max) max=jml;
            }
            System.out.println(bil1 + " " + bil2 + " " +max);
            }
    }
}

0 comments:

Post a Comment

Please leave a comment