-1
#include<stdio.h>
int main()
{
int k,repeticao;
int valor, numero;
int paridade = 0;
scanf("%d", &repeticao);
for( k = 0; k < repeticao; k++ ){
int numero,sequencia;
scanf("%lld", &numero);
valor = numero/2;
while( valor != 0 ){
sequencia = numero%2;
if( sequencia == 1)
{
paridade++;
}
numero = valor;
valor = numero/2;
if (valor == 0)
{
paridade++;
}
}
printf("%d\n", paridade);
paridade = 0;
}
return 0;
}
My code is working for the two entries given in the example: 3 and 21, but in the third entry it returns 15, instead of 50. this third entry is the repetition from 1 to 9 (3 times), and when I repeat it twice(123456789123456789) my output is 1( wrong too) when I put only one repetition(123456789) my output comes out correct ( 16). Can you help me, please?
Do you just want to count bits 1 of an integer? I think the code to do this is much simpler. Just make successive divisions and count when there is rest.
– mau humor
But I am making successive divisions and when the rest is 1 I am increasing the variable "parity". There must be a simpler way, but I can’t see, I’d also like to know why it doesn’t work for all cases the way it is there.
– Vitor Matos
I’ll post my solution.
– mau humor
In Python it would be very easy to do this because the variables have no set size, already in C I believe it is not possible, nor the double supports a number with so many digits, the largest number supported has 16/17 digits since it is a variable of 8 bytes. Then it would not be possible to solve this case.
– Luan Prado
@Luanprado: Don’t say "in C is not possible", no; just do as Python does and use an arbitrary precision library, like the GMP and the MPFR. Incidentally, here is a list.
– Wtrmute