码迷,mamicode.com
首页 > 其他好文 > 详细

C.One Piece

时间:2019-06-01 19:11:03      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:integer   help   problem   const   binary   c++   name   coder   rust   

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

  

C.One Piece

标签:integer   help   problem   const   binary   c++   name   coder   rust   

原文地址:https://www.cnblogs.com/YDDDD/p/10960365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!