2174: Accumulator 计算从0加到n共有多少位发生过变化

2174: Accumulator


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
3s8192K324109Standard
Accumulator can be used to count the number of the objects. Initially, the value of accumulator is set to zero. After each objected is counted, the value of accumulator will increase by one. At the end, the value of accumulator is the total number of the objects counted. The number shown on the accumulator is in decimal form between 0 and 1000000000, however, inside the accumulator, the number is represented using 32bit binaries. The initial value of the accumulator is 32 zeros. Like below

00000000000000000000000000000000

You need to modify the binaries using the following method. Suppose a binary t of the length 32 bit, each bit can be represented with t[i] (0 <= i < 32). Of course, i=0 is the lowest bit.

i=0;
s=0;
while(t[i]!=0)
{
    t[i]=0;
    i++;
    s++;
}
t[i]=1;
s++;
The total bits that need to be modifed to change from current number to next number is the value of s.
EG.
DEC		Bin
8        0000000000000000000000000000111
9        0000000000000000000000000001000 

You need to change 4 bits. The input to this problem is the final result of the accumulator(in decimal format), please calculate how many bits changed during the accumulation.

Sample Input

4

Sample Output

7
赵大牛找的规律,膜拜一下
#include<iostream>
#include<cstdio>
using namespace std;
int count_1(int n)//求n的二进制中有几个1
{
    int cnt;
    for(cnt = 0; n; n >>= 1)
    {
         cnt += n & 1;
     }
    return cnt;
}
int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        int cnt=n*2-count_1(n);
        printf("%d/n",cnt);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值