输入一个正整数,计算它在二进制下的1的个数。
注意多组输入输出!!!!!!
数据范围:
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); int i = Integer.parseInt(s); String binaryString = Integer.toBinaryString(i); int count = 0; for (int j = 0; j < binaryString.length(); j++) { if (binaryString.charAt(j) == '1') { count++; } } System.out.println(count); } } }
public void stringTo62( ){ Scanner in = new Scanner(System.in); while (true){ int a = in.nextInt(); System.out.println(Integer.bitCount(a)); } }
import java.io.*; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { int n = Integer.parseInt(str.trim()); //用于记录1的个数 int count = 0; //int共32位,循环32次 for (int i = 0; i < 32; i++) { //n & 1表示n二进制末位的值&1,结果为1则说明末位为1,结果为0说明末位为0 if ((n & 1) == 1) count++; //n右移一位 n = n >> 1; } 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 int a = in.nextInt(); String str = Integer.toBinaryString(a); int count = 0; for (int j = 0; j < str.length(); j++) { if (String.valueOf(str.charAt(j)).equals("1")) { count++; } } 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 int i=in.nextInt(); String str=Integer.toBinaryString(i); System.out.println(str.replace("0","").length()); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int n = in.nextInt(); //思路一:先转二进制,再求1的数量 /* String str = toBinary(n); System.out.println(str.replace("0","").length()); */ //思路二:在转的过程累加1的数量 System.out.println(getOneCount(n)); } } //思路二方法 public static int getOneCount(int n){ int count = 0; while(n >= 1){ if(n % 2 == 1) count++; n = n/2; } return count; } //求取十进制转二进制字符串 public static String toBinary(int n){ //String str = Integer.toBinaryString(n); StringBuffer buffer = new StringBuffer(); while(n > 1){ buffer.append(n % 2); n = n / 2; } buffer.append(n); //将最后的商拼接 buffer.reverse(); //字符串反转,因为转二进制是反着连的 return buffer.toString(); } }
import java.util.Scanner; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num = in.nextInt(); int res = 0; for (int i = 0; i < 32; i++) // 1依次左移31位,每一次与num进行与运算 if ((num & (1 << i)) != 0) res++; System.out.println(res); } in.close(); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; while((str = br.readLine())!=null){ int num = Integer.parseInt(str); // System.out.println(Integer.toBinaryString(num)); int count = 0; for (int i = 0; i < 32; i++) { //如果末位为1则计数 if ((num & 1) == 1) count++; //无符号右移 num = num >>> 1; } System.out.println(count); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); Scanner scanner = new Scanner(br); int result; while (scanner.hasNext()){ result = countNumberOf1InBinary3(scanner.nextInt()); System.out.println(result); } } // 解法一:通过与1进行&运算并右移,但遇到有符号数会陷入死循环 private static int countNumberOf1InBinary1(int num) { int count = 0; while (num != 0){ if ((num & 1) == 1) count++; num >>= 1; } return count; } // 解法二:通过与 1 进行与运算,并将 1 左移,判断每一位是否为1。 private static int countNumberOf1InBinary2(int n) { int count = 0, flag = 1; while (flag != 0) { if ((n & flag) != 0) count++; flag = flag << 1; } return count; } // 解法三: 一个数减去1后与自身进行与运算,得到的结果相当于把该数二进制最右边的1变为0 private static int countNumberOf1InBinary3(int n) { int count = 0; while (n != 0) { n = (n - 1) & n; count++; } return count; } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ Integer num = new Integer(sc.nextInt()); String res = Integer.toBinaryString(num); String res_ = res.replace("1", ""); System.out.println(res.length() - res_.length()); } } }