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

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

An Easy Problem!

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

#include <iostream>
using namespace std;
 
int main() {
    int sum, mx, temp;
    string s;
    while (getline(cin, s)){
        sum = 0;
        mx = 1;
        int i;
        for (i = 0; i < s.size(); i++) {
            if (s[i] >= '0' && s[i] <= '9') {
                temp = s[i] - '0';
            }
            else if (s[i] >= 'A' && s[i] <= 'Z') {
                temp = s[i] - 'A' + 10;
            }
            else if (s[i] >= 'a' && s[i] <= 'z') {
                temp = s[i] - 'a' + 36;
            }
            else continue;
            if (mx < temp)
                mx = temp;
            sum += temp;
        }
        for (i = mx; i < 62; i++)
            if (!(sum % i)) {
                cout << i + 1 << "\n";
                break;
            }
        if (i == 62)
            cout << "such number is impossible!\n";
    }
    return 0;
}

Fibonaccimal Base

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

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

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
 
int main() {
    vector <int> fib;
    int f0 = 0, f1 = 1, f2 = 1;
    for (; f2 <= 1e8; ){
        f2 = f0 + f1;
        fib.push_back(f2);
        f0 = f1;
        f1 = f2;
    }
    reverse(fib.begin(), fib.end());
    int N, n, x;
    cin >> N;
    while (N--){
        cin >> n;
        x = n;
        vector <int> ans;
        int i = 0;
        while (fib[i] > x) i++;
        for (; i < fib.size(); i++){
            if (fib[i] <= x){
                x -= fib[i];
                ans.push_back(1);
                i++;
                if (i < fib.size()) ans.push_back(0);
            } else ans.push_back(0);
        }
        cout << n << " = ";
        for (auto j: ans) cout << j;
        cout << " (fib)\n";
    }
    return 0;
}

Funny Encryption Method

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

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

#include <iostream>
using namespace std;

int main() {
    int T, N;
    cin >> T;
    while (T--){
        cin >> N;
        int X1 = N;
        int b1 = 0;
        while (X1){
            b1 += X1 & 1;
            X1 >>= 1;
        }
        int X2 = 0;
        int mul = 1;
        X1 = N;
        while (X1){
            X2 += (X1 % 10) * mul;
            X1 /= 10;
            mul *= 16;
        }
        int b2 = 0;
        while (X2){
            b2 += X2 & 1;
            X2 >>= 1;
        }
        cout << b1 << " " << b2 << "\n";
    }
    return 0;
}



留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
0會員
30內容數
Jim的沙龍的其他內容
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
2025/08/14
What is the Probability!! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=997 中文題目:https:
2025/08/14
What is the Probability!! 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=997 中文題目:https:
看更多