码迷,mamicode.com
首页 > 其他好文 > 详细

[HDOJ4349]Xiao Ming's Hope

时间:2015-08-17 17:03:53      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4349

 

数出n的二进制有x位1,再求2的x次幂就可以,数据不大不用优化

 

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 using namespace std;
 6 
 7 typedef long long LL;
 8 
 9 LL solve(int x){
10     int cnt = 0;
11     while(x){
12       if(x % 2)
13           cnt++;
14       x >>= 1;
15     }
16     LL sum = 1;
17     for(int i = 1; i <= cnt; ++i) {
18         sum *= 2;
19     }
20     return sum;
21 }
22 
23 int main(){
24     int n;
25     while(scanf("%d",&n) != EOF){
26        LL ans = solve(n);
27        printf("%d\n", ans);
28     }
29     return 0;
30 }
View Code

 

[HDOJ4349]Xiao Ming's Hope

标签:

原文地址:http://www.cnblogs.com/vincentX/p/4736857.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!