hdu5491 The Next 模拟


LetLLdenote the number of 1s in integerDD’s binary representation. Given two integersS1S1andS2S2, we callDDa WYH number ifS1LS2S1≤L≤S2.
With a givenDD, we would like to find the next WYH numberYY, which is JUST larger thanDD. In other words,YYis the smallest WYH number among the numbers larger thanDD. Please write a program to solve this problem.

InputThe first line of input contains a numberTTindicating the number of test cases (T300000T≤300000).
Each test case consists of three integersDD,S1S1, andS2S2, as described above. It is guaranteed that0D<2310≤D<231andDDis a WYH number.
OutputFor each test case, output a single line consisting of “Case #X: Y”.XXis the test case number starting from 1.YYis the next WYH number.Sample Input

3
11 2 4
22 3 3
15 2 5

Sample Output

Case #1: 12
Case #2: 25
Case #3: 17

题目需要求的是比d大的且转化为二进制后1的个数在s1和s2之间的最小的数

开始想的是从d开始判断yi的个数分比s1小,在s1、s2之间(这里考虑的特别复杂),比s2大三种情况考虑,结果写了一大堆判断最后完美wa

后来在网上看别人的代码,是先让d+1,因为最后得到的数是比d大的,然后也是三种情况考虑,但是如果在s1、s2之间就可以直接输出,接着是比s1小则遇到0直接变成1,比s2大则遇到1变成0然后再一次进位

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
int a[50], j;
ll sum() {
    ll ans = 0, t = 1;
    for( int i = 0; i <= 33; i ++ ) {
        ans = ans + a[i] * t;
        t *= 2;
    }
    return ans;
}
int main() {
    std::ios::sync_with_stdio(false);
    int T, cnt = 0;
    cin >> T;
    while( T -- ) {
        cnt ++;
        ll n, num = 0, x, y;
        j = 0;
        cin >> n >> x >> y;
        n ++;
        memset( a, 0, sizeof(a) );
        while( n ) {
            if( n % 2 == 1 ) {
                a[j++] = 1;
                num ++;
            } else {
                a[j++] = 0;
            }
            n /= 2;
        }
        while( 1 ) {
            if( num >= x && num <= y ) {
                cout << "Case #" << cnt << ": " << sum() << endl;
                break;
            }
            if( num < x ) {
                for( int i = 0; ; i ++ ) {
                    if( a[i] == 0 ) {
                        a[i] = 1;
                        num ++;
                        break;
                    }
                }
            } else {
                int i = 0;
                while( a[i] == 0 ) {
                    i ++;
                }
                a[i] ++;
                while( a[i] == 2 ) {
                    a[i] = 0;
                    num --;
                    a[i+1] ++;
                    i ++;
                }
                num ++;
            }
        }
    }
    return 0;
}

优质内容筛选与推荐>>
1、SVN服务器搭建之提交日志模版构建
2、【转载】HTTP协议详解
3、DataTable转JSON
4、数据库连接类
5、Git的使用


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn