【PAT B-1005】继续

【PAT B-1005】继续,第1张

【PAT B-1005】继续(3n+1)猜想 C++代码
#include 
using namespace std;
using gg = long long;  //类型别名
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    gg ni, ai;
    unordered_set<gg> us;  //存储被覆盖的数
    cin >> ni;
    vector<gg> v;
    while (ni--) {
        cin >> ai;
        v.push_back(ai);
        while (ai != 1) {
            if (ai % 2 == 1)
                ai = ai * 3 + 1;
            ai /= 2;
            us.insert(ai);
        }
    }
    sort(v.begin(), v.end(), greater<gg>());  //从大到小排序
    bool first = true;  //标志是否是第一个输出的数字,控制输出空格
    for (auto i : v) {
        if (not us.count(i)) {  // i是关键数
            cout << (first ? "" : " ") << i;
            first = false;
        }
    }
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/674617.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存