爱买手办的张三

爱买手办的张三

https://ac.nowcoder.com/acm/problem/206648

题目:爱买手办的张三
来源:2020年第十四届山东大学程序设计竞赛(重现赛)

解题思路

题目要求:给定非负整数
的最小值,并递增列出 ;求 的最大值,并递增列出 ,其中

显然,

用二进制表示,为 0 的位置上 在该位置必须为 0,为 1 的位置上 在该位置可以是 1 或者 0。
所以,求出 的二进制表示有 个 1,

C++代码

#include<iostream>
#include<bitset>
#include<vector>
#include<cmath>
using namespace std;

int main(){
    int Y;
    cin >> Y;
    cout << 1 << endl;
    cout << Y << endl;
    bitset<32> y(Y);
    int k = y.count();
    cout << pow(2, k) << endl;
    cout << 0 << endl;
    vector<int> cnt;
    for(int i=0; i<32; ++i){
        int sum = 0;
        if(y[i] == 1){
            sum += pow(2, i);
            cout << sum << endl;
            int n = cnt.size();
            cnt.push_back(sum);
            for(int j=0; j<n; ++j){
                cnt.push_back(sum + cnt[j]);
                cout << cnt.back() << endl;
            }
        }
    }
    return 0;
}
全部评论

相关推荐

2025-12-28 22:19
门头沟学院 Java
不敢追165女神:简历写得毫无特点,你说你要是大二或者大三找寒假实习到暑期实习这段时间,你的简历还能约到面试。但是你是研究生哥,面试官不会因为你是研究生而降低要求,反而会觉得你是研究生才学了这么一点?为什么我不找个同阶段的本科生?
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务