PTA甲 1073~1076题解

1073 Scientific Notation

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 s; cin >> s;
    int idx = s.find('E');
    string x = s.substr(0, idx), y = s.substr(idx + 1);
    if (stod(x) > 0 && x[0] == '+') x = x.substr(1);

    int e = stoi(y);
    if (e > 0) {
        int n = x.size(), o = x.find('.');
        if (n - o - 1 > e) {
            x.insert(x.begin() + o + e + 1, '.');
        } else {
            for (int i = 1; i <= e - n + o + 1; i++) x += "0";
        }
        x.erase(x.begin() + o);
    } else if (e < 0) {
        e = abs(e);
        x.erase(x.begin() + x.find('.'));
        if (x[0] == '-') {
            x = x.substr(1);
            y = "";
            for (int i = 1; i <= e - 1; i++) y += "0";
            x = "-0." + y + x;
        } else {
            y = "";
            for (int i = 1; i <= e - 1; i++) y += "0";
            x = "0." + y + x;
        }
    }

    cout << x << '\n';

    return 0;
}

1074 Reversing Linked List

模拟 没必要手写链表.

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

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

    unordered_map<string, int> val;
    unordered_map<string, string> ne;

    string head;
    int n, K;
    cin >> head >> n >> K;
    for (int i = 0; i < n; i++) {
        string add, nex;
        int data;
        cin >> add >> data >> nex;
        val[add] = data;
        ne[add] = nex;
    }

    vector<tuple<string, int, string>> v, res;
    while (true) {
        v.emplace_back(head, val[head], ne[head]);
        head = ne[head];
        if (head == "-1") break;
    }
    // 加了这一行 就过了最后一个测试点
    n = v.size();
    
    for (int i = 0; i < n; i++) {
        if (i + K - 1 < n) {
            for (int j = i + K - 1; j >= i; j--) {
                res.emplace_back(v[j]);
            }
            i += K - 1;
        } else {
            for (int j = i; j < n; j++) {
                res.emplace_back(v[j]);
            }
            break;
        }
    }

    for (int i = 0; i < n - 1; i++) {
        get<2>(res[i]) = get<0>(res[i + 1]);
    }
    get<2>(res.back()) = "-1";

    for (const auto& t : res) {
        cout << get<0>(t) << ' ' << get<1>(t) << ' ' << get<2>(t) << '\n';
    }

    return 0;
}

1075 PAT Judge

又是答辩排序模拟 最恶心的地方是编译过算0分,全没编译过的人,不加入排行。

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

struct Student {
    int id, cnt, tot;
    bool ok;
    array<int, 10> sol{};
    explicit Student(int _id): id(_id), cnt(0), ok(false), tot(0) {
        fill(sol.begin(), sol.end(), -2);
    }
};

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

    unordered_map<int, int> score[10]; // 每道题每个学生
    array<int, 10> eves{}; // 每道题满分

    int N, K, M; cin >> N >> K >> M;
    for (int i = 1; i <= K; i++) cin >> eves[i];

    while (M--) {
        int id, so, sc; cin >> id >> so >> sc;
        if (sc == -1) {
            if (!score[so].count(id)) score[so][id] = -1;
            continue;
        }
        score[so][id] = max(score[so][id], sc);
    }

    vector<Student> res;
    for (int i = 1; i <= N; i++) {
        Student student(i);
        for (int j = 1; j <= K; j++) {
            if (score[j].count(i)) {
                student.sol[j] = score[j][i];
                if (student.sol[j] ^ -1) student.ok = true;
                if (student.sol[j] ^ -1) student.tot += score[j][i];
                if (student.sol[j] == eves[j]) ++student.cnt;
            }
        }
        if (student.ok) res.emplace_back(student);
    }

    sort(res.begin(), res.end(), [](const Student& lhs, const Student& rhs) -> bool {
        if (lhs.tot == rhs.tot) {
            if (lhs.cnt == rhs.cnt) return lhs.id < rhs.id;
            return lhs.cnt > rhs.cnt;
        }
        return lhs.tot > rhs.tot;
    });

    auto print = [&K](const Student& stu) -> void {
        cout << setfill('0') << setw(5) << stu.id;
        cout << ' ' << stu.tot;
        for (int i = 1; i <= K; i++) {
            cout << ' ';
            if (stu.sol[i] == -2) cout << '-';
            else if (stu.sol[i] == -1) cout << 0;
            else cout << stu.sol[i];
        }
        cout << '\n';
    };

    cout << 1 << ' ';
    print(res[0]);
    int pre = res[0].tot, rk = 1;
    for (int i = 1; i < res.size(); i++) {
        if (res[i].tot == pre) {
            cout << rk << ' ';
        } else {
            pre = res[i].tot;
            rk = i + 1;
            cout << rk << ' ';
        }
        print(res[i]);
    };

    return 0;
}

1076 Forwards on Weibo

you are supposed to calculate the maximum potential amount of forwards for any specific user, assuming that only L levels of indirect followers are counted。我没懂啊,什么是间接粉丝。原来是除了博主的,都算间接粉丝,呵呵。
bfs计数,顺便存起来,防止 K K K过大。

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

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

    int n, L; cin >> n >> L;
    vector<vector<int>> graph(n + 1);
    for (int i = 1; i <= n; i++) {
        int num; cin >> num;
        while (num--) {
            int x; cin >> x;
            graph[x].push_back(i);
        }
    }
    
    unordered_map<int, int> res;
    auto bfs = [&graph, &res, L](int start) -> void {
        array<int, 1010> vis{};
        vis[start]++;
        queue<int> qu;
        qu.push(start);
        int lev = -1, cnt = 0;
        while (!qu.empty()) {
            ++lev;
            if (lev > L) break;
            auto sz = qu.size();
            if (lev > 0) cnt += sz;
            while (sz--) {
                int f = qu.front();
                qu.pop();
                for (int x : graph[f]) {
                    if (vis[x]) continue;
                    vis[x]++;
                    qu.push(x);
                }
            }
        }
        res[start] = cnt;
    };

    int K; cin >> K;
    while (K--) {
        int s; cin >> s;
        if (res.count(s)) cout << res[s] << '\n';
        else {
            bfs(s);
            cout << res[s] << '\n';
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值