链接:https://ac.nowcoder.com/acm/contest/908/C

题意:

Luffy once saw a particularly delicious food, but he didn't have the money, so he asked Nami for money. But we all know that Nami can't easily give money to Luffy. Therefore, Nami decided to take a test of Luffy. If Luffy was right, Nami would give him money, otherwise Luffy would only be hungry. The problem is as follows: Give you an 32-bit integer n and find the number of 1 in the binary representation of the integer.
You are the most trusted friend of Luffy, and he is now asking for help.

思路:

二进制找1,负数用1<<32 减一下就行。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int MAXN = 3e5 + 10;
const int MOD = 1e9 + 7;
LL n, m, k, t; int main()
{
cin >> t;
while (t--)
{
int sum = 0;
cin >> n;
if (n < 0)
n = (1LL<<32)+n;
while (n)
{
if (n&1)
sum++;
n >>= 1;
}
cout << sum << endl;
} return 0;
}

  

05-11 19:41