Questions binary counting algorithm training
Resource limitation
time limit: 1.0s memory limit: 256.0MB
problem description
given L, R. Statistics [L, R] for all numbers in the section included in the binary "1" and the number.
5 is binary as 101, comprises two "1."
Input format
The first line contains the number of 2 L, R
output format of
a number S, represents [L, R] for all numbers included in the section at the binary number "1" and.
Input Sample
23
Sample Output
3
data size and conventions
L <= R <= 100000;
import java.util.Scanner;
public class 二进制数数 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int l=sc.nextInt();
int r=sc.nextInt();
int count = 0,a,b;
for (int i=l;i<=r;i++){
a=i;
while (a!=0){
b=a%2;
if (b==1) count++;
a/=2;
}
}
System.out.println(count);
}
}