攀拓(PAT)- 程序设计(乙级)2023年夏季考试仿真卷题解

发布时间 2023-07-10 16:05:31作者: Xgoat

B-1 唯手熟尔

思路:getchar一个读入到string中进行判断
代码:

#include<bits/stdc++.h>

using i64 = long long;

#pragma GCC optimize(2)

#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)

int main()
{
    IOS;

    char op;
    int ans = 0;
    std::string s;
    while (true){
        op = getchar();
        if (op == '#') {
            break;
        }
        s += op;
    }
    
    for (int i = 0; i < s.size(); i ++){
        if (s[i] == 'f'){
            std::string fs = s.substr(i, 5);

            if (fs == "fight"){
                ans ++;
            }
        }
    }
    std::cout << ans << "\n";

    return 0;
}

B-2 构造性证明

思路:全排列 dfs next_permutation 找出第一组即可
代码:

#include<bits/stdc++.h>

using i64 = long long;

#pragma GCC optimize(2)

#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)


int main()
{
    IOS;

    int n = 5, a, b, c, d;

    std::vector <std::pair <int, double>> ans;
    for (int i = 1; i <= n; i ++){
        double x;
        std::cin >> x;

        ans.push_back({i, x});
    }

    int _ans = 0;
    do{
        if (fabs(ans[0].second / ans[1].second - ans[2].second / ans[3].second) < 0.5){
            if (_ans == 0){
                a = ans[0].first;
                b = ans[1].first;
                c = ans[2].first;
                d = ans[3].first;
            }
            _ans ++;
        }
    }while (std::next_permutation(ans.begin(), ans.end()));
    

    std::cout << _ans << " " << a << " " << b << " " << c << " " << d << "\n"; 

    return 0;
}

B-3 大数A+B

思路:找出最大数确定进制 在进行高精度加法算出答案
代码:

#include<bits/stdc++.h>

using i64 = long long;

#pragma GCC optimize(2)

#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)


int main()
{
    IOS;

    int m, n, maxx = 0;
    std::cin >> m;

    std::vector <int> a(10010, 0);
    for (int i = m; i >= 1; i --){
        std::cin >> a[i];
        maxx = std::max(a[i], maxx);
    }

    std::cin >> n;
    
    std::vector <int> b(10010, 0);
    for (int i = n; i >= 1; i --){
        std::cin >> b[i];
        maxx = std::max(b[i], maxx);
    }
    maxx ++;

    int t = 0;
    std::vector <int> ans;
    for (int i = 1; i <= std::max(m, n) or t; i ++){
        if (i <= std::max(m, n)) t = (a[i] + b[i] + t);

        // std::cout << t << "\n";
        ans.push_back(t % maxx);
        t /= maxx;
    }

    std::cout << ans.size() << " ";

    reverse(ans.begin(), ans.end());

    for (int i = 0; i < ans.size(); i ++){
        std::cout << ans[i] << " \n"[i == ans.size() - 1];
    }

    return 0;
}

B-4 陷阱

思路:大模拟题 读懂题目即可
代码:

#include<bits/stdc++.h>

using i64 = long long;

#pragma GCC optimize(2)

#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)

int main()
{
    IOS;

    int n;
    std::cin >> n;

    std::vector <std::vector <int>> g(n + 2, std::vector <int> (n + 2, 0));
    for (int i = 1; i <= n; i ++){
        for (int j = 1; j <= n; j ++){
            char x;
            std::cin >> x;

            g[i][j] = x - '0';
        }
    }

    int st, en;
    std::vector <int> ans;
    for (int i = 1; i <= n; i ++){
        st = n + 1;
        en = i;
        bool ok = true;
        while (true){
            if (st > 0 and g[st - 1][en] == 0){
                st -= 1;
            }else if (en > 0 and g[st][en - 1] == 0){
                en -= 1;
            }else if (st == 0 or en == 0){
                break;
            }else {
                ok = false;
                break;
            }
        }
        if (ok == false){
            ans.push_back(i);
        }
    }

    for (int i = 0; i < ans.size(); i ++){
        std::cout << ans[i] << " \n"[i == ans.size() - 1];
    }

    return 0;
}

B-5 用两个堆栈实现队列

思路:两个栈模拟即可 画图理解
代码:

#include<bits/stdc++.h>

using i64 = long long;

#pragma GCC optimize(2)

#define IOS std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr)

int main()
{
    IOS;

    std::stack <int> s1, s2;
    int _, t = 0;
    std::cin >> _;

    while (_ --){
        char op;
        int x;
        std::cin >> op;

        if (op == 'I'){
            std::cin >> x;

            s1.push(x);
        }else {
            if (s2.size()){
                t ++;
                std::cout << s2.top() << " " << t << "\n";
                s2.pop();
                t = 0;
            }else if (s1.size()){
                while (s1.size()){
                    s2.push(s1.top());
                    s1.pop();
                    t += 2;
                }

                t ++;
                std::cout << s2.top() << " " << t << "\n";
                s2.pop();
                t = 0;
            }else {
                std::cout << "ERROR\n";
            }
        }
    }

    return 0;
}