This is the tale of Zephyr, the greatest time traveler theworld will never know. Even those who are aware of Zephyr's existence know verylittle about her. For example, no one has any clue as to which time period sheis originally from.
But we do know the story of the first time she set out tochart her own path in the time stream. Zephyr had just finished building hertime machine which she named - "Dokhina Batash". She was making thefinal adjustments for her first trip when she noticed that a vital program wasnot working correctly. The program was supposed to take a number N, andfind what Zephyr called its Onoroy value.
The Onoroy value of an integer N is the number of onesin its binary representation. For example, the number 13 (11012) hasan Onoroy value of 3. Needless to say, this was an easy problem for the greatmind of Zephyr. She solved it quickly, and was on her way.
You are now given a similar task. Find the first numberafter N which has the same Onoroy value as N.
Input
Input starts with an integer T (≤ 65),denoting the number of test cases.
Each case begins with an integer N (1 ≤ N ≤109).
Output
For each case of input you have to print the case number andthe desired result.
Sample Input | Output for Sample Input |
5 23 14232 391 7 8 | Case 1: 27 Case 2: 14241 Case 3: 395 Case 4: 11 Case 5: 16 |
上来直接暴力做:#include<stdio.h>
int value(int n)
{ int b,v=0;
while(n!=0)
{b=n%2;
n/=2;
v+=b;}
return v;
}
int main()
{
int t,n,a,i=1,m;
scanf("%d",&t);
while(t--)
{scanf("%d",&n);
a=n+1;m=value(n);
while(1)
{if(value(a)==m)break;
else
a++;}
printf("Case %d: %d\n",i,a);
i++;
}
return 0;
}
TLE;
再问人。。。从二进制下手,编了半天出不来啊,烦!