• C.One Piece


    链接: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;
    }
    

      

  • 相关阅读:
    UVA756
    SP30906
    SP32900
    CF940F
    洛谷P5030
    洛谷P5142
    洛谷P2569
    网络流 24 题做题记录
    矩阵
    二分图
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10960365.html
Copyright © 2020-2023  润新知