题解:P9239 [蓝桥杯 2023 省 B] 填空问题

· · 题解

试题 A

其实不需要手动枚举,直接写程序就行了。

#include<bits/stdc++.h>
using namespace std;
int main(){
    vector<int> arr = {5, 6, 8, 6, 9, 1, 6, 1, 2, 4, 9, 1, 9, 8, 2, 3, 6, 4, 7, 7,5, 9, 5, 0, 3, 8, 7, 5, 8, 1, 5, 8, 6, 1, 8, 3, 0, 3, 7, 9,2, 7, 0, 5, 8, 8, 5, 7, 0, 9, 9, 1, 9, 4, 4, 6, 8, 6, 3, 3,8, 5, 1, 6, 3, 4, 6, 7, 0, 7, 8, 2, 7, 6, 8, 9, 5, 6, 5, 6,1, 4, 0, 1, 0, 0, 9, 4, 8, 0, 9, 1,2, 8, 5, 0, 2, 5, 3, 3};
    int days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    set<string> valid_dates;
    int n = arr.size();
    for (int i = 0; i < n; i++) {
        if (arr[i] != 2) continue;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] != 0) continue;
            for (int k = j + 1; k < n; k++) {
                if (arr[k] != 2) continue;
                for (int l = k + 1; l < n; l++) {
                    if (arr[l] != 3) continue;
                    for (int m = l + 1; m < n; m++) {
                        if (arr[m] != 0 && arr[m] != 1) continue;
                        for (int n1 = m + 1; n1 < n; n1++) {
                            int month = arr[m] * 10 + arr[n1];
                            if (month < 1 || month > 12) continue;
                            for (int o = n1 + 1; o < n; o++) {
                                if (arr[o] < 0 || arr[o] > 3) continue;
                                for (int p = o + 1; p < n; p++) {
                                    int day = arr[o] * 10 + arr[p];
                                    if (day >= 1 && day <= days_in_month[month]) {
                                        string date = (month < 10 ? "20230" : "2023") + to_string(month) + (day < 10 ? "0" : "") + to_string(day);
                                        valid_dates.insert(date);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    cout << "找到 " << valid_dates.size() << " 个不同的2023年日期:" << endl;
    return 0;
}

运行输出 235 个,试题 A 答案为 235。

试题 B

同样的,我们直接编写程序。题目相当于让我们去找使 H 最接近 11625907.5798 的 m 的值。

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n = 23333333;
    double target = 11625907.5798;
    long long best_m = 0;
    double min_diff = 1e18;
    for (long long m = 1; m <= n / 2; m++) {
        double p0 = (double)m / n;
        double p1 = (double)(n - m) / n;
        double term1 = (m * m) / (double)n;
        double term2 = ((n - m) * (n - m)) / (double)n;

        double log_p0 = log2(p0);
        double log_p1 = log2(p1);

        double H = - (term1 * log_p0 + term2 * log_p1);

        double diff = fabs(H - target);
        if (diff < min_diff) {
            min_diff = diff;
            best_m = m;
        }
    }
    cout << "最佳匹配 m = " << best_m << endl;
    return 0;
}

运行输出 11027421,试题 B 答案为 11027421。

AC 代码

#include<iostream>
using namespace std;
int main() {
    string ans [] = {
        "235",
        "11027421",
    };
    char T;
    cin >> T;
    cout << ans[T - 'A'] << endl;
    return 0;
}

感谢阅读。