Problem C
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 167 Accepted Submission(s) : 64
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
给定一个正整数N,求最小的、比N的的正整数M,使得M与N的二进制表示中M含有的1比N含有的1多1个。如给定N为78,其二进制表示为1001110,包含4个1,那么最小的比N大的且二进制包括5个1的数是79,其二进制是1001111。
Input
输入一个正整数N(1<=n<=100000).有多个测试用例。
Output
输出对应的M数。
Sample Input
1
Sample Output
3
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int A(int x)
{
int s=0;
while(x)
{
if(x%2==1)
s++;
x/=2;
}
return s;
}
int main()
{
int n,m;
while(~scanf("%d",&n))
{
int y=A(n);
for(int i=n;;i++)
{
if(A(i)>y)
{
printf("%d\n",i);
break;
}
}
}
return 0;
}