http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=2679 這提示輸入時進位整數顯示二進位中有幾個一,Integer.bitCount()剛好符合要求
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n=scn.nextInt();
System.out.printf("The number of bits is %d.\n",Integer.bitCount(n));
}
/*
題目:[C_MM108-易] 位元計數器
作者:1010
時間:西元 2016 年 7 月 */
}