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: