zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    【脑图】iOS的Crash分类和捕获
    Ruby03
    Ruby02
    Ruby01
    如何快速把一个十进制数转换为二进制?
    iOS
    互联网协议基本知识
    XCBB
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/YDDDD/p/10960365.html
Copyright © 2011-2022 走看看