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

更新 發佈閱讀 6 分鐘

A mid-summer night’s dream

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

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

#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
 
int main() {
    int n;
    while (cin >> n){
        int a[n];
        for (int i = 0; i < n; i++){
            cin >> a[i];
        }
        sort(a, a+n);
        int mid1 = a[(n-1)/2];
        int mid2 = a[n/2];
        int ans = 0;
        for (int i = 0; i < n; i++){
            if (a[i] == mid1 || a[i] == mid2) ans++;
        }
        cout << mid1 << " " << ans << " " << mid2 - mid1 + 1 << "\n";
    }
    return 0;
}

Tell me the frequencies!

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

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

#include <iostream>
#include <algorithm>
using namespace std;
#define pii pair<int,int>
 
bool cmp(pii a, pii b){
    if (a.first != b.first) return a.first < b.first;
    else return a.second > b.second;
}
 
int main() {
    string s;
    while (getline(cin, s)) {
        pii a[256];
        for (int i = 0; i < 256; i++) {
            a[i] = {0, i};
        }
        for (int i = 0; i < s.size(); i++) {
            a[(int)s[i]].first++;
        }
        sort(a, a+256, cmp);
        for (auto i: a){
            if (i.first > 0) cout << i.second << " " << i.first << "\n";
        }
        cout << "\n";
    }
    return 0;
}

Train Swapping

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

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

#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T, N;
    cin >> T;
    while (T--){
        cin >> N;
        int a[N];
        for (int i = 0; i < N; i++){
            cin >> a[i];
        }
        int cnt = 0;
        for (int i = 0; i < N-1; i++){
            for (int j = i+1; j < N; j++){
                if (a[i] > a[j]){
                    swap(a[i], a[j]);
                    cnt++;
                }
            }
        }
        cout << "Optimal train swapping takes " << cnt << " swaps.\n";
    }
    return 0;
}


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