Don't like ads? PRO users don't see any ads ;-)
Guest

sorlaker p1 3n+1

By: a guest on Jul 4th, 2012  |  syntax: None  |  size: 0.78 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.Scanner;
  2. class   P1{
  3.         public static int count(int number){
  4.                 int conta = 1;
  5.                 while(number != 1){
  6.                         if (number%2 == 0){
  7.                                 number = number/2;
  8.                         }else{
  9.                                 number = 3*number + 1;
  10.                         }
  11.                         conta++;
  12.                 }
  13.                 return conta;
  14.         }
  15.         public static void main(String[] args){
  16.                
  17.                 Scanner e = new Scanner(System.in);
  18.                 int a, b, maior = 0; //rs = condicao ternaria de contaA e contaB
  19.                 while(e.hasNext()){
  20.                         a = e.nextInt();
  21.                         b = e.nextInt();
  22.                         int[] numeros = new int[b - a + 1];
  23.                         int indice = 0;
  24.                         for (int x = a; x <= b; x++){
  25.                                 numeros[indice++] = count(x);
  26.                         }              
  27.                         maior = numeros[0];
  28.                         for (int x = 0; x < numeros.length; x++){
  29.                                 if (numeros[x] > maior){
  30.                                         maior = numeros[x];
  31.                                 }
  32.                         }
  33.                         System.out.println(a+" "+b+" "+maior);
  34.                 }
  35.         }
  36. }