Codeforces 1037A Packets (二进制位数)

题目链接

http://codeforces.com/contest/1037/problem/A

题意

给定n个硬币,每个硬币的币值为1.
将硬币打包,每个包要么不用要么全用。
求组成1,2,3,…,n的最少的包数。

题解

求数n的二进制位数即可。

AC代码

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;

int countbit(int x)
{
    int ans=0;
    while(x)
    {
        x>>=1;
        ans++;
    }
    return ans;
}
int main()
{
    int n;
    while(cin>>n)
    {
        cout<<countbit(n)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37685156/article/details/82351421