can anyone help me with this Java problem?

0

Description

The numbers in their binary representation are formed by ones and zeros. We want to know how many pairs of followed ones exist in a number. For example, the number 710 in binary is 1112 and there is a pair of ones. The number 310 in binary is 112 has a pair of ones. The number 1510 in binary is 11112 and has two pairs of ones. The number 1010 in binary is 10102 has no pairs of ones.

Given a decimal number less than 225, count how many pairs of ones exist in their binary representation.

Entry

The input consists of several test cases, the first line contains a number that indicates the number of test cases. Each test case consists of a decimal number less than 225.

Exit

Write in the output the number of pairs of ones that have the number.

Example Entry

5
3
10
15
20
255

Example Exit

1
0
2
0
4

My code is this:

import java.util.Scanner;

public class ejercPares {

    public static void imprimeEscalera(int M) {
        int exp, digito;
        double binario;
        Scanner sc = new Scanner(System.in);
        do {
        } while (M < 0);
        exp = 0;
        binario = 0;
        while (M != 0) {
            digito = M % 2;
            binario = binario + digito * Math.pow(10, exp);
            exp++;
            M = M / 2;
        }
        int co = 0;
        int co2 = 0;
        int co3 = 0;
        for (int i = 0; i <= 1000; i++) {
            co = (int) (binario % 10);

            if (binario == 0) {
                break;
            }
            if (co == 1) {
                co2++;
            }
            co3 = (int) (binario / 10);
            binario = co3;
        }
        System.out.println(co2 / 2);
        co2 = co2 * 0;
        binario = binario * 0;
    }

    public static void main(String[] args) {
        int N;
        Scanner lee = new Scanner(System.in);
        while (lee.hasNext()) {
            N = lee.nextInt();
            int j = 0;
            while (j <= N) {
                j++;
                int M = lee.nextInt();
                imprimeEscalera(M);
            }
        }
    }
}

By sending the judge I get this:

    
asked by DALM10 18.04.2018 в 20:07
source

1 answer

0

Taking into account the part of your statement:

  

Given a decimal number less than 225 count how many pairs of ones   they exist in their binary representation

I leave a script with some test cases descriptive method that first calculates the binary representation of the decimal number and then calculates the number of repetitions of "11" in that binary representation, you must adapt it to your code:

int []casosDePrueba = {3,5,10,15,20,255};
String binario;

for(int caso:casosDePrueba){
    binario = Integer.toBinaryString(caso);//calculo la representacion binaria
    System.out.println(caso+" en binario es: "+binario);

    int index = 0;
    int count = 0;

    while((index = binario.indexOf("11", index)) != -1){
        index += 2;//corro dos espacios por el length de "11" para la nueva busqueda
        count++;
    }

    System.out.println("repeticiones de '11' encontradas:"+count);
    System.out.println("-----------------------");
}

Additionally I leave a sandbox so you can see it working

    
answered by 18.04.2018 в 21:15