PTA甲 1061~1064题解

1061 Dating

b u l l s h i t bullshit bullshit 模拟题

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    string days[7] = {"MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"};

    map<char, int> mp;
    for (int i = 0; i <= 9; i++) {
        mp[char('0' + i)] = i;
    }
    int t = 10;
    for (char i = 'A'; i <= 'N'; i++) mp[i] = t++;

    string s1, s2, s3, s4, res;
    cin >> s1 >> s2 >>s3 >> s4;

    int id = 0;
    for (int i = 0; i < min(s1.size(), s2.size()); i++) {
        if (s1[i] == s2[i]) {
            if (id == 1 && (s1[i] >= 'A' && s1[i] <= 'N' || s1[i] >= '0' && s1[i] <= '9')) {
                int x = mp[s1[i]];
                res += (x < 10 ? "0" : "") + to_string(x) + ":";
                break;
            }
            if (id == 0 && s1[i] >= 'A' && s1[i] <= 'G') {
                res += days[s1[i] - 'A'] + " ";
                id ^= 1;
            }
        }
    }

    for (int i = 0; i < min(s3.size(), s4.size()); i++) {
        if ((s3[i] >= 'A' && s3[i] <= 'Z' || s3[i] >= 'a' && s3[i] <= 'z') && s3[i] == s4[i]) {
            if (i < 10) res += "0";
            res += to_string(i) + "\n";
            break;
        }
    }

    cout << res;

    return 0;
}

1062 Talent and Virtue

b u l l s h i t bullshit bullshit 排序模拟题

#include <bits/stdc++.h>
using namespace std;

struct Person {
    string id;
    int vir, tal;
    explicit Person(const string& s, int _vir, int _gra): id(s), vir(_vir), tal(_gra) {}
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int n, L, H; cin >> n >> L >> H;
    vector<Person> sage, noble, fool, rest;
    for (int i = 0; i < n; i++) {
        string id; cin >> id;
        int vir, tal; cin >> vir >> tal;
        if (vir < L || tal < L) continue;

        if (vir >= H && tal >= H) {
            sage.emplace_back(id, vir, tal);
        } else if (vir >= H && tal < H) {
            noble.emplace_back(id, vir, tal);
        } else if (vir >= tal) {
            fool.emplace_back(id, vir, tal);
        } else rest.emplace_back(id, vir, tal);
    }

    auto cmp = [](const Person& A, const Person& B) -> bool {
        if (A.vir + A.tal == B.vir + B.tal) {
            if (A.vir == B.vir) return A.id < B.id;
            return A.vir > B.vir;
        }
        return A.vir + A.tal > B.vir + B.tal;
    };


    sort(sage.begin(), sage.end(), cmp);
    sort(noble.begin(), noble.end(), cmp);
    sort(fool.begin(), fool.end(), cmp);
    sort(rest.begin(), rest.end(), cmp);

    cout << sage.size() + noble.size() + fool.size() + rest.size() << '\n';

    for (const auto& p : sage) {
        cout << p.id << ' ' << p.vir << ' ' << p.tal << '\n';
    }
    for (const auto& p : noble) {
        cout << p.id << ' ' << p.vir << ' ' << p.tal << '\n';
    }
    for (const auto& p : fool) {
        cout << p.id << ' ' << p.vir << ' ' << p.tal << '\n';
    }
    
    for (const auto& p : rest) {
        cout << p.id << ' ' << p.vir << ' ' << p.tal << '\n';
    }

    return 0;
}

1063 Set Similarity

求并集并计数 用哈希表模拟就行了。

#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int N; cin >> N;
    vector<unordered_map<int, int>> v;
    for (int i = 0; i < N; i++) {
        int n; cin >> n;
        unordered_map<int, int> mp;
        while (n--) {
            int t; cin >> t;
            mp[t]++;
        }
        v.emplace_back(mp);
    }

    int K; cin >> K;
    while (K--) {
        int a, b; cin >> a >> b;
        --a, --b;
        unordered_map<int, int> uni;
        double cnt = 0;
        const auto& m1 = v[a], &m2 = v[b];
        for (const auto& [key, _] : m1) {
            if (m2.count(key)) ++cnt;
            uni[key]++;
        }
        for (const auto& [key, _] : m2) uni[key]++;
        double res = (cnt / double(uni.size())) * 100;
        cout << fixed << setprecision(1) << res;
        cout << "%\n";
    }

    return 0;
}

1064 Complete Binary Search Tree

考察完全二叉树的性质 和 二叉搜索的中序序列就是一个递增序列
先排序 然后dfs建树 然后输出

#include <bits/stdc++.h>
using namespace std;

static int tr[3000];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);

    int n; cin >> n;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; i++) cin >> v[i];

    sort(v.begin() + 1, v.end());
    
    int index = 0;
    function<void(int)> dfs = [&](int u) -> void {
        if (u > n) return;
        dfs(u << 1);
        tr[u] = v[++index];
        dfs(u << 1 | 1);
    };
    dfs(1);

    for (int i = 1; i <= n; i++) {
        if (i == n) cout << tr[i];
        else cout << tr[i] << ' ';
    }
    cout << '\n';

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值