LightOJ 1042 Secret Origins(贪心或者位运算)

Secret Origins

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 numberN, 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 asN.

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

思路:调正顺序后,从低位向高位找到第一个01交换为10,找到之前要统计出1和0的个数,最后在这个10后面把统计出的0用完,再把统计出的1用完。

特殊情况:一直找不到01的,比如110,10,100,这种情况使得array[0]必须是0,数字的二进制形式也必须从array[1]开始填。那么110变成0110,交换01就是1010,又有1个0和1个1使用,替换进去分别是1000,1001。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
int digit[100];
int dd[100];
int main()
{
    int t,n,m,i,j;
    scanf("%d",&t);
    for(int o=1;o<=t;o++)
    {
        scanf("%d",&n);
        m=n;
        i=0;
        while(m!=0)
        {
            digit[++i]=m%2;
            m=m/2;
        }
        dd[0]=0;
        for(int k=1;k<=i;k++)
            dd[k]=digit[i-k+1];
        //已经求出了遍历后的东西了 然后现在要做的就是 从后面查找那个为01那么对对调一下
        int len=i;
        int cnt0=0,cnt1=0;
        int pos;
        for(i=len;i>=0;i--)
        {
            if(dd[i]==1&&dd[i-1]==0)
            {
                dd[i]=0;
                dd[i-1]=1;
                pos=i+1;
                break;
            }
            else
            {
                if(dd[i]==1)cnt1++;
                else if(dd[i]==0)cnt0++;
            }
        }
        for(j=0;j<cnt0;j++)
              dd[pos++]=0;
        for(j=0;j<cnt1;j++)
              dd[pos++]=1;
       int x=dd[len]*1;
       int a=1;
       for(j=len-1;j>=0;j--)
       {
           a=a*2;
           x=x+a*dd[j];
       }
     printf("Case %d: %d\n",o,x);
    }
	return 0;
}

可参考以下原文章
————————————————
版权声明:本文为CSDN博主「galesaur_wcy」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/galesaur_wcy/article/details/78162819

发布了11 篇原创文章 · 获赞 0 · 访问量 139

猜你喜欢

转载自blog.csdn.net/weixin_45670020/article/details/104092477