PAT(乙级)2020年冬季考试

比赛链接:https://pintia.cn/market/item/1336191032829964288

7-1 祖传好运 (15 分)

题解

递归判断即可。

代码

#include <bits/stdc++.h>
using namespace std;
bool ok(int x) {
    if (x <= 9) {
        return true;
    } else {
        return ok(x / 10) and x % to_string(x).length() == 0;
    }
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        cout << (ok(n) ? "Yes" : "No") << "
";
    }
    return 0;
}

7-2 找奇葩 (20 分)

题解

模拟即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        ++mp[x];
    }
    for (auto it : mp) {
        if (it.first % 2 == 1 and it.second % 2 == 1) {
            cout << it.first << "
";
        }
    }
    return 0;
}

7-3 舍入 (20 分)

题解

需要考虑:

  • 有负号的情况
  • 没有小数点的情况
  • 需要一直进位的情况,如 9.9999
  • -0.00001 保留一位小数,应为 0.0
  • 保留的位数比浮点数长的情况,如 1.0 保留三位小数应为 1.000

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, d;
    cin >> n >> d;
    for (int i = 0; i < n; i++) {
        int op;
        string s;
        cin >> op >> s;
        string sign;
        if (s.front() == '-') {
            sign = '-';
            s.erase(0, 1);
        }
        int pos = s.find('.');
        if (pos == -1) {
            s += '.';
            pos = s.find('.');
        }
        s += string(100, '0');
        string t = s.substr(0, pos + 1 + d);
        if (op == 1) {
            if (s[pos + d + 1] >= '5') {
                ++t.back();
            }
        } else if (op == 3) {
            if (s[pos + d + 1] > '5') {
                ++t.back();
            } else if (s[pos + d + 1] == '5') {
                if (any_of(s.begin() + pos + d + 2, s.end(), [](char c) { return c != '0'; })) {
                    ++t.back();
                } else if ((t.back() - '0') % 2 == 1) {
                    ++t.back();
                }
            }
        }
        t = '0' + t;
        for (int i = t.length() - 1; i > 0; i--) {
            if (t[i] == '9' + 1) {
                t[i] = '0';
                ++t[i - 1];
            } else if (t[i] == '.' + 1) {
                t[i] = '.';
                ++t[i - 1];
            }
        }
        if (t.front() == '0') {
            t.erase(0, 1);
        }
        cout << (all_of(t.begin(), t.end(), [](char c) { return c == '0' or c == '.'; }) ? t : sign + t) << "
";
    }
    return 0;
}

7-4 最近的斐波那契数 (20 分)

题解

模拟即可。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    int a = 0, b = 1;
    while (b < n) {
        int na = b, nb = a + b;
        a = na;
        b = nb;
    }
    cout << (abs(n - a) <= abs(n - b) ? a : b) << "
";
    return 0;
}

7-5 子串与子列 (25 分)

题解

枚举两遍子串的起点即可。
第一遍得到包含子序列的子串的最短长度,第二遍输出满足题意的最靠左的最短子串。

代码

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    string s;
    cin >> s;
    string p;
    cin >> p;
    string ans;
    int min_len = INT_MAX;
    for (int i = 0; i < s.size(); i++) {
        int p1 = i, p2 = 0;
        while (p1 < s.size() and p2 < p.size()) {
            while (p1 < s.size() and p2 < p.size() and s[p1] != p[p2]) {
                ++p1;
            }
            if (p1 < s.size() and p2 < p.size() and s[p1] == p[p2]) {
                ++p1;
                ++p2;
            }
        }
        if (p2 == p.size() and p1 - i < min_len) {
            min_len = p1 - i;
            ans = s.substr(i, min_len);
        }
    }
    cout << ans << "
";
    return 0;
}

参考博客

https://blog.csdn.net/qq_43196686/article/details/112038777

原文地址:https://www.cnblogs.com/Kanoon/p/14531141.html