在一行上输入一个整数
,代表给定的数字。
在一行上输出一个整数,代表
的二进制表示中
的个数。
10
2
十进制
到
的二进制表示如下:
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
。
0
0
简单位运算问题,用Integer.toBiaryString(n)把十进制n转换为二进制字符串,遍历字符串统计字符‘1’的个数,最后输出控制台即可。
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
String s = Integer.toBinaryString(n);
int res = 0;
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == '1'){
res++;
}
}
System.out.println(res);
}
}
} import java.util.Scanner;
import java.math.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger a = in.nextBigInteger();
String s1 = a.toString(2);
s1 = s1.replace("0", "").trim();
System.out.println(s1.length());
}
} BigInteger 处理转二进制
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
String str = Integer.toBinaryString(num);
int sum = 0;
for(int i=0;i<str.length();i++){
sum = sum + Integer.valueOf(str.charAt(i)+"");
}
System.out.println(sum);
} import java.util.Scanner;
import java.math.BigInteger;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int a = in.nextInt();
BigInteger integer = new BigInteger(String.valueOf(a));
String binaryOutput = integer.toString(2);
int count = 0;
for(int i=0;i<binaryOutput.length();i++){
if(binaryOutput.charAt(i)-'0'==1){
count +=1;
}
}
System.out.print(count);
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = in.nextInt();
int count = 0; //记录1出现的次数
//10进制整数转换成2进制:用十进制数除以 2,得到商和余数。
// 将余数记录下来,这个余数就是二进制数的最低位。
// 用得到的商继续除以 2,重复上述步骤,直到商为 0。
// 从下往上读取所有的余数,得到的就是十进制数整数部分对应的二进制数
while (n > 0) {
if (n % 2 == 1) {
count = count + 1;
}
n = n / 2;
}
System.out.println(count);
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
System.out.println(Integer.bitCount(in.nextInt()));
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int i = in.nextInt();
int count = 0;
while(i>0){
if((i & 1) == 1){
count++;
}
i = i >> 1;
}
System.out.print(count);
}
} import java.util.Scanner;
public class Main11 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()) {
int number = in.nextInt();
String str = Integer.toBinaryString(number);
String times1 = str.replace("0", "");
System.out.println(times1.length());
}
in.close();
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
int count = 0;
while (num > 0) { // 不断除二取余即可
count += num % 2;
num /= 2;
}
System.out.print(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = Integer.toBinaryString(in.nextInt());
String noOne = str.replace("1", "");
System.out.print(str.length() - noOne.length());
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// System.out.println("请输入您要计算的数:");
int n = sc.nextInt();
System.out.println(Integer.bitCount(n));
}
} 方法二:转化成字符数组计算字符1出现的次数。 // Scanner sc = new Scanner(System.in);
// System.out.println("请输入要计算的数字:");
// int n = sc.nextInt();
// String binaryString = Integer.toBinaryString(n);
// char[] charArray = binaryString.toCharArray();
// int count = 0;
// for (char c : charArray) {
// if(c == '1'){
// count++;
// }
// }
// System.out.println(count); import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int count = 0;
while (num > 0) {
if (num % 2 != 0) {
count++;
}
num = num >> 1;
}
System.out.println(count);
}
}