Ir ao conteúdo
  • Cadastre-se

JBGP

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

Reputação

0
  1. JBGP

    Java Onde está o erro?

    Pessoal, recebi a seguinte questão para resolver em Java. Contudo, não consigo identificar onde está o erro. Nos testes apresentados, o código sequer passa pelo primeiro. Consider the following algorithm to generate a sequence of numbers. Start with an integer n. If n is even, divide it by 2. If n is odd, multiply it by 3 and add 1. Repeat this process with the new value of n, ending when n = 1. For example, the following sequence of numbers will be generated when n is 22: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1 Although there is still no proof, mathematicians believe that this algorithm always ends with n = 1, for any integer n. Well, for this problem here at Huxley, this property holds for any integer less than 1,000,000. For an input n, the "cycle size" of n is the number of numbers generated up to 1 (including 1 itself). In the example above, the "cycle size" of 22 is 16. Given two numbers i and j, determine the maximum "cycle size" of all numbers between i and j, including both i and j. Input The input consists of a series of pairs of integers i and j, with a pair of integers per line. All integers will be less than 1,000,000 and greater than 0. Note that the input ends only when there are no more numbers. Find out how to make your program work in this case. Each language has a different way of reading while there is still input to be read. Output For each pair of integers i and j, print i and j in the same order in which they appear in the input and then print the maximum "cycle size" found. These 3 numbers must be separated by a blank space, with all 3 numbers in a line and with one output line for each input line. See the sample file to better understand the input and output format. input Samples. Output Samples 1 10 1 10 20 100 200 100 200 125 201 210 201 210 89 900 1000 900 1000 174 package br.p1.atividade08; import java.util.Scanner; public class Question03 { public static void main(final String[] args) { Scanner input = new Scanner (System.in); while(input.hasNext()){ int maxCyclesize = 0; int firstNum = input.nextInt(); int finalNum = input.nextInt(); if(firstNum > finalNum){ int aux = firstNum; firstNum = finalNum; finalNum = aux; } int numAtual = firstNum; while(numAtual <= finalNum){ int cyclesize = 1; int n = numAtual; while(n != 1){ if(n%2 == 0){ n = n/2; cyclesize++; }else if(n%2 != 0){ n = n*3 +1; cyclesize++; } } if(cyclesize > maxCyclesize){ maxCyclesize = cyclesize; } } System.out.printf("%d %d %d\n", firstNum, finalNum, maxCyclesize); } } }

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!