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 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


题意:给一个数,求与这个数的二进制含有相同1的个数并且比这个数大的最小的数;

思路:两种方法。第一种,我们可以直接贪心+模拟,贪心策略,如果一个数的二进制里面含有01这样一个序列,那么应该吧从右往左的第一个这样的序列交换位置,然后吧这个序列后面的1全部移到最后,例如10011011000变为10011100001,如果不存在01序列,则把第一位变成0,再在最前面补一个1,并且把原先第一位后面的全部的1移到最后,比如1100,变成10001,111变成1011;;第二种,细心一点可以发现,变换后的排列即位原来序列的下一个排列。可以用库函数直接求下一个排列,但要注意当前排列时最大的排列这种情况。下面给出两份代码:

第一种:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<stack>
#include<set>
#include<algorithm>
#define LL long long
using namespace std;
LL n;
int arr[1000];
int base[33];
void GetBase()
{
    base[0]=1;
    for(int i=1;i<=32;i++)
        base[i]=2*base[i-1];
}
LL solve(LL x)
{
    int cnt=0;
    while(x)
    {
        arr[++cnt]=x%2;
        x/=2;
    }//注意求得arr序列时倒着的
    int flag=0,sum=0,p,_sum;
    for(int i=1;i<cnt;i++)
    {
        if(arr[i]==1&&arr[i+1]==0&&!flag)
        {
            swap(arr[i],arr[i+1]);flag=1;p=i;_sum=sum;//_sum记录10序列后面右多少个1
        }
        if(arr[i]==1)
            sum++;
    }
    if(flag)
    {
        for(int i=1;i<p;i++)
        {
            if(_sum) arr[i]=1,_sum--;
            else arr[i]=0;
        }
    }
    else
    {
        arr[cnt++]=0;
        arr[cnt]=1;
        for(int i=1;i<cnt;i++)
        {
            if(sum) arr[i]=1,sum--;
            else arr[i]=0;
        }
    }
    LL ans=0;
    for(int i=1;i<=cnt;i++)
        ans+=arr[i]*base[i-1];
    return ans;
}
int main()
{
    int t,cas=0;
    scanf("%d",&t);
    GetBase();
    while(t--)
    {
        scanf("%lld",&n);
        printf("Case %d: %lld\n",++cas,solve(n));
    }
    return 0;
}
第二种:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<stack>
#define inf 0x7fffffff
using namespace std;
priority_queue<int> que;
long long int n;
string arr;
long long base[35];
void Getbase()
{
    base[0]=1;
    for(int i=1;i<=33;i++)
        base[i]=base[i-1]*2;
}
long long  int f(long long int n)
{
    string str;
    while(n)
    {
        if(n & 1)
            str += '1';
        else
            str += '0';
        n >>= 1;
    }
    str += '0';
    reverse(str.begin(), str.end());//倒置
    next_permutation(str.begin(), str.end());//求下一个排列
    long long int ans = 0;
    for(int i=0; i<str.size(); ++ i)
        ans= ans*2+( str[i] - '0');
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    Getbase();
    while(t--)
    {
        scanf("%lld",&n);
        printf("Case %d: %lld\n",++cas,f(n));
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值