提交时间:2024-08-19 21:58:00
运行 ID: 168035
#include <iostream> #include <string> using namespace std; string toBinary(int n) { string binary = ""; while (n > 0) { binary = (n % 2 == 0 ? "0" : "1") + binary; n /= 2; } return binary.empty() ? "0" : binary; } int main() { int a, b; cin >> a >> b; if (a > b) { swap(a, b); } int countA = 0, countB = 0; for (int i = a; i <= b; ++i) { string binary = toBinary(i); int count1 = 0, count0 = 0; for (char bit : binary) { if (bit == '1') { count1++; } else { count0++; } } if (count1 > count0) { countA++; } else { countB++; } } cout << countA << " " << countB << endl; return 0; }