1 solutions

  • 0
    @ 2025-5-24 13:46:16
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        while(cin>>n,n)
        {
            int ans=0;
            int t=n;
            while(t) //计算n中1的个数 
            {
                if(t&1) ans++;
                t/=2;
            }
            for(int i=n+1;;i++)
            {
                int t=i;
                int cnt=0;
                while(t) //计算i的二进制中1的个数 
                {
                    if(t&1)
                    {
                        cnt++;
                    }
                    t/=2; 
                }
                if(cnt==ans) //找到了大于n的且二进制1相同的数的个数 
                {
                    cout<<i<<endl;
                    break;
                }
            }
        }
        return 0;
    }
    
    • 1

    Information

    ID
    245
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    3
    Tags
    # Submissions
    3
    Accepted
    3
    Uploaded By