第一行输入一个整数
代表需要求解的第一个数字。
第二行输入一个整数
代表需要求解的第二个数字。
第一行输出一个整数,代表
在二进制表示下的
的个数。
第二行输出一个整数,代表
在二进制表示下的
的个数。
5 0
2 0
十进制
到
的二进制表示如下:
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
;
十进制
等于二进制
。
本题数据已进行规范,不再需要读入至文件结尾(2025/01/09)。
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n = scan.nextInt();
int count = 0;
String str = Integer.toBinaryString(n);
char[] cha = str.toCharArray();
for(char c :cha){
if(c=='1'){
count++;
}
}
System.out.println(count);
}
}
}
//
// main.cpp
// 题目:
//
// 算法:
// Created by Rain on 16/03/2017.
// Copyright © 2017 Rain. All rights reserved.
//
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
int a=0;
while(cin>>a)
{
int b=0;
while(a)
{
a&=a-1;
b++;
}
cout<<b<<endl;
}
return 0;
}
//一种简单的求法
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner input=new Scanner(System.in);
while (input.hasNext()) {
int n = input.nextInt();
int count = 0;
while (n > 0){
n = n & (n-1);
count ++;
}
System.out.println(count);
}
}
}
#include<iostream>
using namespace std;
int main()
{
int num;
while(cin >> num)
cout << __builtin_popcount(num) << endl;
return 0;
} import java.util.*;
public class Main{
public static void main(String []args){
Scanner sc = new Scanner(System.in);//先扫描进来一段字符串
while(sc.hasNext()){//循环扫描
int count = 0;//计数1的个数
int n = sc.nextInt();//把字符串转换为整型
String r = Integer.toBinaryString(n);//十进制整型再转换为二进制字符串
for (int i = 0; i <= r.length() - 1; i++) {
if (r.charAt(i) == '1') {//遍历这个二进制字符串,找到字符1就记数
count ++;
}
}
System.out.println(count);//输出计数
}
}
} #include<stdio.h>
int main()
{
int num,cnt;
while(scanf("%d",&num)!=EOF)
{
cnt=0;
while(num>0)
{
if(num%2==1)
cnt++;
num = num/2;
}
printf("%d\n",cnt);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int count = 0, num = scanner.nextInt();
while (num != 0) {
count++;
num &= num - 1;
}
System.out.println(count);
}
}
} while True:
try:
print(format(int(input().strip()), 'b').count('1'))
except:
break import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
System.out.println(Integer.bitCount(scan.nextInt()));
}
scan.close();
}
}
差不多一行代码解决,利用Integer类bitCount函数,找出一个整型数2进制位有多少个1
//使用位运算快速的将十进制转为2进制
#include<bits/stdc++.h>
using namespace std;
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("E:/input.txt", "r", stdin);
#endif
int n;
while(cin >> n)
{
int ans = 0;
for(int i = 31; i >= 0; --i)
{
ans += (n >> i & 1);
}
cout << ans << endl;
}
return 0;
}
//应用Integer.bitCount()
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int num = scanner.nextInt();
System.out.println(Integer.bitCount(num));
}
scanner.close();
}
}