Neko Performs Cat Furrier Transform

題目: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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章