JOJ2243:Endless Carry

来源:互联网 发布:it编程培训中心靠谱吗 编辑:程序博客网 时间:2024/05/16 06:28

传送门:http://acm.jlu.edu.cn/joj/showproblem.php?pid=2243

很好的一道数学题,我们可以看出,二进制某一位上的1进位次数是2 ^ i - 1(i是1的位置),所以统计二进制数1的个数就行了。

代码:

#include <iostream>using namespace std;int main(){       int n,ans;       while (cin>>n && n)       {              ans=n;              do                     ans-=n&1;              while (n>>=1);              cout<<ans<<endl;       }       return 0;}


原创粉丝点击