题目:Neko Performs Cat Furrier Transform

题意:本题的题意就是给你一个数,你把它变成变成二进制,然后通过两种操作把它变成全1的形式,然后输出偶数的操作步数的n是多少。
思路:本题的思路就是通过先把最高位的0置为1,然后再重新寻找最高位的0.
代码:

#include<bits/stdc++.h>
using namespace std;

set<int>s;
vector<int>v;
int main()
{
    int x;
    for(int i = 1; i <= 100000000; i *= 2 )s.insert(i);
    scanf("%d", &x);
    int ans = 0;
    while(s.count(x+1) == 0)
    {
        if(ans & 1)
        {
            x++;
            ans++;
        }
        else
        {
            ans ++;
            int base = 1, res = 1, cnt = 1, tp;
            while(base < x)
            {
                if(!(x & base))res = base, tp = cnt;
                base <<= 1;
                cnt++;
            }
            res <<= 1;
            x ^= (res-1);
            v.push_back(tp);
        }

    }
    printf("%d\n", ans);
    if(v.size())
    {
        for(int i = 0; i < v.size(); i++)
        {
            if(i != 0)cout<<" ";
            printf("%d", v[i]);
        }
        printf("\n");
    }
    return 0;
}

posted on 2019-07-18 09:10  Refused  阅读(49)  评论(0)    收藏  举报