文章

7

粉丝

319

获赞

5

访问

71.4k

头像
不断除以2,看最后一位的奇偶
P1008 华南师范大学/贵州大学机试
发布于2020年8月9日 12:48
阅读数 9.4k

#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define debug  freopen("in.txt","r",stdin),freopen("out.txt","w",stdout);
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e6+10;
const int maxM = 1e6+10;
const int inf = 0x3f3f3f3f;

int N;
int main(){
	// debug;
	// ios;

	cin>>N;
	int zero = 0,one = 0;
	for(int i = 0;i<32;i++){
		if(N & 1) one++;
		else zero++;
		N/=2;
	}
	printf("count0=%d count1=%d",zero,one);

	

	return 0;
}
登录查看完整内容


登录后发布评论

2 条评论
WangKunRui
2024年1月4日 17:08

void getBinaryNums(int nums){
    int counts[2] = {0};
    if(nums == 0) counts[0] = 1;
    while(nums != 0){
        nums%2?counts[1]++:counts[0]++;
        nums = nums/2;
    }
    printf("count0=%d count1=%d", counts[0], counts[1]);
}

赞(1)
月溅星河
2023年12月22日 10:51

写的不错wink

赞(1)