LightOJ - 1042 Secret Origins (模拟)水

LightOJ - 1042
Secret Origins
Time Limit: 500MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

Description

This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr's existence know very little about her. For example, no one has any clue as to which time period she is originally from.

But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - "Dokhina Batash". She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.

The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.

You are now given a similar task. Find the first number after 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 and the desired result.

Sample Input

5

23

14232

391

7

8

Sample Output

Case 1: 27

Case 2: 14241

Case 3: 395

Case 4: 11

Case 5: 16

Source

Problem Setter: Muntasir Muzahid Chowdhury
Special Thanks: Jane Alam Jan
//题意:
给你一个数n,让你找到最小的比n大并且它俩的二进制数的表示中1的个数相同,输出这个数即可
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int num[1010];
int main()
{
	int t,k=1;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		int temp=n;
		int cnt=0;
		memset(num,0,sizeof(num));
		while(temp)
		{
			num[cnt++]=temp%2;
			temp/=2;
		}
		bool f=false,f2=false;;
		int cout=0;
		int s=0;
		int pre=-1,now=-1;
		for(int i=0;i<cnt-1;i++)
		{
			if(num[i]==1)
				pre=now,now=i,cout++;
			if(num[i]==1&&num[i+1]==0)
			{
				s=i;
				swap(num[i],num[i+1]);
				f=true;
				break;
			}
		}
		if(cout==cnt-1)
		{
			int sum=0;
			for(int i=0;i<cnt-1;i++)
			sum+=pow(2,i);
			sum+=pow(2,cnt);
			printf("Case %d: %d\n",k++,sum);
		}
		else
		{
			if(f)
			{
				int sum=0;
				for(int i=s;i<cnt;i++)
				if(num[i])
				sum+=pow(2,i);
				cout--;
				while(cout)
				{
					cout--;
					sum+=pow(2,cout);
				}
				printf("Case %d: %d\n",k++,sum);
			}
			else
			{
				int sum=0;
				sum+=pow(2,cnt);
				while(cout>=0)
				{	
					cout--;
					sum+=pow(2,cout);
				}
				printf("Case %d: %d\n",k++,sum);
			}		
		}
	}
	return 0;
}

你可能感兴趣的:(LightOJ - 1042 Secret Origins (模拟)水)