2 solutions

  • 1
    @ 2025-7-31 10:41:36
    
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	int n;
    	while(cin>>n,n)
    	{
    		bitset<32> a(n);
    		for(int i=n+1;;i++)
    		{
    			bitset<32> b(i);
    			if(a.count()==b.count())
    			{
    				cout<<i<<endl;
    				break;
    			}
    		}
    	}
        return 0;
    }
    • 1
      @ 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
      15
      Accepted
      11
      Uploaded By