CF 579A (二进制拆分)

本文提供了一个算法,用于确定在细胞培养皿中加入最少数量的细胞,以便在某一天细胞数量恰好等于指定的目标值。通过计算目标值的二进制表示中的1的数量来实现这一目标。

在培养皿中,每个细胞每天会繁殖,数量*2
我们可以在任意天加入任意数量的细胞入培养皿中。
想要知道最少加入多少个细胞,可以使得有一天,培养皿中细胞的数量会恰好为x

其实就是输出X的二进制中有多少个1

Sample test(s)
input
5
output
2
input
8
output
1

 

 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <string>
 6 # include <cmath>
 7 # include <queue>
 8 # include <list>
 9 # define LL long long
10 using namespace std ;
11 
12 
13 
14 int main()
15 {
16     //freopen("in.txt","r",stdin) ;
17     int n ;
18     while(scanf("%d",&n) != EOF)
19     {
20         int sum = 0 ;
21         while(n)
22         {
23             if (n & 1)
24                 sum++ ;
25             n >>= 1 ;
26         }
27         printf("%d\n" , sum) ;
28     }
29 
30 
31 
32     return 0;
33 }
View Code

 

转载于:https://www.cnblogs.com/mengchunchen/p/4836617.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值