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

更新於 發佈於 閱讀時間約 7 分鐘

Parity

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

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

#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
    int I, n;
    while (cin >> I) {
        if (I == 0) break;
        string B = "";
        n = I;
        int cnt = 0;
        while (n){
            cnt += (n & 1);
            B += '0' + (n & 1);
            n >>= 1;
        }
        reverse(B.begin(), B.end());
        cout << "The parity of " << B << " is " << cnt << " (mod 2).\n";
    }
    return 0;
}

Cheapest Base

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

#include <iostream>
#include <map>
using namespace std;
 
int main() {
    int T, cost, Q, x;
    cin >> T;
    for (int Case = 1; Case <= T; Case++){
        if (Case > 1) cout << "\n";
        map <int, int> mp;
        for (int i = 0; i < 36; i++){
            cin >> cost;
            mp[i] = cost;
        }
        cout << "Case " << Case << ":\n";
        cin >> Q;
        while (Q--){
            cin >> x;
            cout << "Cheapest base(s) for number " << x << ":";
            int mn =  0x7FFFFFFF;
            map <int,int> ans;
            for (int i = 2; i <= 36; i++){
                int n = x;
                cost = 0;
                while (n){
                    cost += mp[n % i];
                    n /= i;
                }
                ans[i] = cost;
                mn = min(mn, cost);
            }
            for (int i = 2; i <= 36; i++){
                if (ans[i] == mn) cout << " " << i;
            }
            cout << "\n";
        }
    }
    return 0;
}

Hartals

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

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

#include <iostream>
#include <cstring>
using namespace std;
 
int main() {
    int T, N, P, h;
    cin >> T;
    while (T--){
        cin >> N >> P;
        int a[N+1];
        memset(a, 0, sizeof(a));
        for (int i = 0; i < P; i++){
            cin >> h;
            for (int j = h; j <= N; j+=h){
                a[j] = 1;
            }
        }
        for (int i = 6; i <= N; i+=7){
            a[i] = 0;
        }
        for (int i = 7; i <= N; i+=7){
            a[i] = 0;
        }
        int sum = 0;
        for (int i = 1; i <= N; i++){
            sum += a[i];
        }
        cout << sum << "\n";
    }
    return 0;
}


留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
1會員
31內容數
Jim的沙龍的其他內容
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
2025/08/14
Odd Sum 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724 中文題目:https://zerojudge.tw/Sh
2025/08/14
Odd Sum 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724 中文題目:https://zerojudge.tw/Sh
看更多