C.One Piece

Links: https://ac.nowcoder.com/acm/contest/908/C

Meaning of the questions:

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.

Ideas:

Find binary 1, with 1 << 32 minus negative on the line.

Code:

#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;
}

  

Guess you like

Origin www.cnblogs.com/YDDDD/p/10960365.html