2174: Accumulator
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 324 | 109 | Standard |
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;
}