Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!-C. Johnny and Another Rating Drop(思维,二进制)

题目链接

题意:

给你一个数字n,求1~n的数字相邻两位的二进制之间有多少位不同。

思路:

首先每加1,肯定会有至少一个位置不同,但有些情况会使得大于等于1的位置数字发生变化,经过分析,是碰到绝对偶数(2的次方倍)的情况时,不同的位置数就会+1,所以我们要求这个在小于等于n的情况下能够放下多少绝对偶数。

代码:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=5e5+5;
const int inf=0x3f3f3f3f;
signed main()
{
    IOS;
    int t;
    cin>>t;
    while(t--)
    {
        int n,num=0;
        cin>>n;
        while(n>0)
        {
            num+=n;
            n/=2;
        }
        cout<<num<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ACkingdom/article/details/106578383