洛谷-二进制数转换和对比&

在这里插入图片描述

#include <iostream>
using namespace std;
int A, B, cnt0, cnt1;

int main() 
{
    
    
	int n, t, temp;
	cin >> n;
	for (int i = 1; i <= n; i++) {
    
    
		cnt0 = 0, cnt1 = 0;
		t = i;
		while (t) {
    
    
			temp = t & 1;
			t = t >> 1;
			if (temp)
				cnt1++;
			else
				cnt0++;
		}
		if(cnt1 > cnt0)
		{
    
    
    		A++;
    	}
    	else
    	{
    
    
    		B++;
		}
	}
	cout << A << ' ' << B;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_52045928/article/details/121548161