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

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

Eb Alto Saxophone Player

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

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

#include <iostream>
#include <map>
#include <vector
using namespace std;
 
int main() {
    map <char, vector<int> > mp;
    mp['c'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1};
    mp['d'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0};
    mp['e'] = {0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0};
    mp['f'] = {0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0};
    mp['g'] = {0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0};
    mp['a'] = {0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0};
    mp['b'] = {0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0};
    mp['C'] = {0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0};
    mp['D'] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0};
    mp['E'] = {0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0};
    mp['F'] = {0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0};
    mp['G'] = {0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0};
    mp['A'] = {0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0};
    mp['B'] = {0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};
     
    int t;
    string s;
    cin >> t;
    getline(cin, s);
    while (t--){
        getline(cin, s);
        int cnt[11] = {0};
        int a[11] = {0}; //status
        for (int i = 0; i < s.size(); i++){
            for (int j = 1; j <= 10; j++){
                if (mp[s[i]][j]){
                    if (a[j]) continue;
                    else {
                        a[j] = 1;
                        cnt[j]++;
                    }
                } else {
                    a[j] = 0;
                }
            }
        }
        for (int i = 1; i <= 10; i++){
            cout << cnt[i] << " ";
        }
        cout << "\n";
    }
    return 0;
}

Mutant Flatworld Explorers

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

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

#include <iostream>
#include <map>
using namespace std;

int a[55][55];
int dx[] = {0, 1, 0, -1}; //N, E, S, W
int dy[] = {1, 0, -1, 0};
 
int main() {
    int row, col, R, C, D, nR, nC;
    char d;
    bool lost = false;
    string s;
    map <char, int> mp1;
    map <int, char> mp2;
    mp1['N'] = 0; mp2[0] = 'N';
    mp1['E'] = 1; mp2[1] = 'E';
    mp1['S'] = 2; mp2[2] = 'S';
    mp1['W'] = 3; mp2[3] = 'W';
    cin >> row >> col;
    while (cin >> R >> C >> d >> s){
        D = mp1[d];
        lost = false;
        for (int i = 0; i < s.size(); i++){
            if (s[i] == 'F'){
                nR = R + dx[D];
                nC = C + dy[D];
                if (nR >= 0 && nR <= row && nC >= 0 && nC <= col){
                    R = nR;
                    C = nC;
                } else {
                    if (a[R][C] == 1){
                        continue;
                    } else {
                        cout << R << " " << C << " " << mp2[D] << " LOST\n";
                        lost = true;
                        a[R][C] = 1;
                        break;
                    }
                }
            } else if (s[i] == 'R'){
                D = (D + 1) % 4;
            } else {
                D = (D - 1 + 4) % 4;
            }
        }
        if (!lost) cout << R << " " << C << " " << mp2[D] << "\n";
    }
    return 0;
}

Cola

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

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

#include <iostream>
using namespace std;
 
int main() {
    int N;
    while (cin >> N){
        int ans = N;
        while (N >= 3){
            ans += N / 3;
            N = N / 3 + N % 3;
        }
        ans += (N == 2);
        cout << ans << "\n";
    }
    return 0;
}

Sort! Sort!! and Sort!!!

題目:https://onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=2296

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

#include <iostream>
#include <algorithm>
using namespace std;

int N, M;
 
bool cmp(int x, int y){
    if ((x % M) != (y % M)){
        return (x % M) < (y % M);
    } else {
        if ((x % 2) * (y % 2)){
            return x > y;
        } else if ((x % 2 == 0) && (y % 2 == 0)){
            return x < y;
        } else {
            return (x % 2);
        }
    }
}
         
int main() {
    while (cin >> N >> M){
        if (N == 0 && M == 0){
            cout << "0 0\n";
            break;
        }
        cout << N << " " << M << "\n";
        int a[N];
        for (int i = 0; i < N; i++){
            cin >> a[i];
        }
        sort(a, a+N, cmp);
        for (int i = 0; i < N; i++){
            cout << a[i] << '\n';
        }
    }
return 0;
}



留言
avatar-img
留言分享你的想法!
avatar-img
Jim的沙龍
0會員
30內容數
Jim的沙龍的其他內容
2025/08/14
Hardwood Species 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1167 中文題目:https://zeroju
2025/08/14
Hardwood Species 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1167 中文題目:https://zeroju
2025/08/14
A mid-summer night’s dream 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=998 中文題目:https
2025/08/14
A mid-summer night’s dream 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=998 中文題目:https
2025/08/14
Satellites 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1162 #include <iostream> #incl
2025/08/14
Satellites 題目:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1162 #include <iostream> #incl
看更多