OpenJudge1455 An Easy Problem

来源:互联网 发布:便签数据损坏 编辑:程序博客网 时间:2024/06/04 00:28

题目:http://noi.openjudge.cn/ch0406/1455/
分析:可得lowbit x=x&-x;n+lowbit(n)即把右数第一串连续1消除并在其前添加一个1,(n^(n+lowbit))/lowbit)>>2即为把剩余1放到末尾。
代码:

#include <cstdio>#include <algorithm>#include <cstring>using namespace std;int n;int main(){    int lowbit;    while(scanf("%d",&n)==1&n!=0)    {        lowbit=n&-n;        printf("%d\n",n+lowbit+(((n^(n+lowbit))/lowbit)>>2));    }    return 0;}
0 0
原创粉丝点击