From 4634308d50b5b1e4f993c495a8bb51edac9f94bd Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Thu, 28 May 2026 17:21:08 +0800 Subject: 41-balance --- 41/balance.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 41/balance.cc (limited to '41') diff --git a/41/balance.cc b/41/balance.cc new file mode 100644 index 0000000..b379f0d --- /dev/null +++ b/41/balance.cc @@ -0,0 +1,39 @@ +#include +using namespace std; + +bool is_balance(int a) { + int one_num = 0; + int zero_num = 0; + while (a != 0) + { + if ((a & 1) == 0) { + zero_num++; + } else { + one_num++; + } + a = a >> 1; + } + if (one_num == zero_num) { + return true; + } + return false; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(nullptr); + int n; + cin >> n; + vector a(n); + for (int i = 0; i < n; ++i) { + cin >> a[i]; + } + int cnt = 0; + for (int i = 0; i < n; ++i) { + if (is_balance(a[i])) { + cnt++; + } + } + cout << cnt << endl; + return 0; +} \ No newline at end of file -- cgit v1.2.3