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

更新 發佈閱讀 10 分鐘

Hardwood Species

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

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

#include <iostream>
#include <iomanip>
#include <map>
using namespace std;
 
int main() {
    int n;
    string s;
    cin >> n;
    getline(cin, s);
    getline(cin, s);
    while (n--){
        map <string, int> mp;
        int sum = 0;
        while (getline(cin, s) && s != ""){
            mp[s]++;
            sum++;
        }
        for (auto i: mp){
            cout << i.first << " " <<
            fixed << setprecision(4) << (double)i.second / sum * 100 << "\n";
        }
        cout << "\n";
    }
    return 0;
}

Minesweeper

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

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

#include <iostream>
#include <cstring>
using namespace std;
char a[105][105];
int b[105][105];
int dx[] = {1, -1, 0, 0, -1, 1, -1, 1};
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
 
int main() {
    int n, m, Case = 1;
    string s;
    while (cin >> n >> m){
        if (n == 0 && m == 0) break;
        for (int i = 0; i < n; i++){
            cin >> s;
            for (int j = 0; j < m; j++){
                a[i][j] = s[j];
            }
        }
        memset(b, 0, sizeof(b));
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (a[i][j] == '*'){
                    b[i][j] = -1;
                } else {
                    for (int k = 0; k < 8; k++){
                        int x = i + dx[k];
                        int y = j + dy[k];
                        if (x >= 0 && x < n && y >= 0 && y < m && a[x][y] == '*')
                            b[i][j]++;
                    }
                }
            }
        }
        if (Case > 1) cout << "\n";
        cout << "Field #" << Case++ << ":\n";
        for (int i = 0; i < n; i++){
            for (int j = 0; j < m; j++){
                if (b[i][j] == -1) cout << '*';
                else cout << b[i][j];
            }
            cout << "\n";
        }
    }
    return 0;
}

Die Game

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

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

#include <iostream>
#include <map>
using namespace std;
 
int main() {
    int n;
    string s;
    while (cin >> n){
        if (n == 0) break;
        map <string, int> mp;
        mp["top"] = 1;
        mp["north"] = 2;
        mp["west"] = 3;
        mp["east"] = 4;
        mp["south"] = 5;
        mp["bottom"] = 6;
 
        while (n--){
            cin >> s;
            if (s == "east"){
                int tmp = mp["east"];
                mp["east"] = mp["top"];
                mp["top"] = mp["west"];
                mp["west"] = mp["bottom"];
                mp["bottom"] = tmp;
            }
            if (s == "south"){
                int tmp = mp["south"];
                mp["south"] = mp["top"];
                mp["top"] = mp["north"];
                mp["north"] = mp["bottom"];
                mp["bottom"] = tmp;
            }
            if (s == "west"){
                int tmp = mp["west"];
                mp["west"] = mp["top"];
                mp["top"] = mp["east"];
                mp["east"] = mp["bottom"];
                mp["bottom"] = tmp;
            }
            if (s == "north"){
                int tmp = mp["north"];
                mp["north"] = mp["top"];
                mp["top"] = mp["south"];
                mp["south"] = mp["bottom"];
                mp["bottom"] = tmp;
            }
        }
        cout << mp["top"] << "\n";
    }
    return 0;
}




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