【CPE】一顆星選集(Part 11) | C++

更新 發佈閱讀 7 分鐘

All You Need Is Love!

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1134

中文題目:https://zerojudge.tw/ShowProblem?problemid=d306

#include <iostream>
using namespace std;
 
int mygcd(int x, int y){
    while ((x %= y) && (y %= x));
    return x + y;
}
 
int main() {
    int N;
    string S1, S2;
    cin >> N;
    for (int Case = 1; Case <= N; Case++){
        cin >> S1 >> S2;
        int n1 = 0, n2 = 0;
        for (int i = 0; i < S1.size(); i++){
            n1 *= 2;
            n1 += S1[i] - '0';
        }
        for (int i = 0; i < S2.size(); i++){
            n2 *= 2;
            n2 += S2[i] - '0';
        }
        cout << "Pair #" << Case;
        if (mygcd(n1, n2) > 1) cout << ": All you need is love!\n";
        else cout << ": Love is not all you need!\n";
    }
    return 0;
}

Divide, But Not Quite Conquer!

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1131

中文題目:https://zerojudge.tw/ShowProblem?problemid=e566

#include <iostream>
#include <vector>
using namespace std;
 
int main() {
    int n, m;
    while (cin >> n >> m){
        vector <int> ans;
        bool boring = false;
        if (n == 0 || m == 0) boring = true;
        else {
            ans.push_back(n);
            while (n > 1) {
                if (n%m == 0){
                    n /= m;
                    ans.push_back(n);
                } else {
                    boring = true;
                    break;
                }
            }
        }
        if (boring) cout << "Boring!\n";
        else {
            for (auto i: ans) cout << i << " ";
            cout << "\n";
        }
    }
    return 0;
}

Simply Emirp

題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1176

中文題目:https://zerojudge.tw/ShowProblem?problemid=d387

#include <iostream>
using namespace std;
int p[1000005];
 
int main() {
    p[1] = 1;
    for (int i = 2; i < 1000005; i++){
        if (p[i] == 0){
            for (int j = i+i; j < 1000005; j+=i){
                p[j] = 1;
            }
        }
    }
    int N, N1, N2;
    while (cin >> N){
        N1 = N;
        N2 = 0;
        while (N1){
            N2 *= 10;
            N2 += N1 % 10;
            N1 /= 10;
        }
        if (p[N]) cout << N << " is not prime.\n";
        else if (N2 != N && p[N2] == 0) cout << N << " is emirp.\n";
        else cout << N << " is prime.\n";
    }
    return 0;
}


留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
1會員
31內容數
Jim的沙龍的其他內容
2025/08/14
Parity 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1872 中文題目:https://zerojudge.tw/Sho
2025/08/14
Parity 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1872 中文題目:https://zerojudge.tw/Sho
2025/08/14
An Easy Problem! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1034 #include <iostream>
2025/08/14
An Easy Problem! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1034 #include <iostream>
2025/08/14
Square Numbers 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2456 中文題目:https://zerojudg
2025/08/14
Square Numbers 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2456 中文題目:https://zerojudg
看更多