2023年第14届蓝桥杯大赛软件赛省赛 C/C++ 大学 B 组
试题 A: 日期统计(5)
直接暴力,8个for + 优化,2~5分钟跑完。
答案:365
点击查看代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n = 100, a[110], st[13][33];
int main() {
freopen("in.txt", "r", stdin);
freopen("out2.txt", "w", stdout);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int y1 = 1; y1 <= n-7; y1++) {
if (a[y1] != 2) continue;
for (int y2 = 2; y2 <= n-6; y2++) {
if (a[y2] != 0) continue;
for (int y3 = 3; y3 <= n-5; y3++) {
if (a[y3] != 2) continue;
for (int y4 = 4; y4 <= n-4; y4++) {
if (a[y4] != 3) continue;
for (int m1 = 5; m1 <= n-3; m1++) {
for (int m2 = 6; m2 <= n-2; m2++) {
int mm = a[m1] * 10 + a[m2];
if (mm < 1 || mm > 12) continue;
for (int d1 = 7; d1 <= n-1; d1++) {
for (int d2 = 8; d2 <= n; d2++) {
int dd = a[d1] * 10 + a[d2];
if (dd >= 1 && dd <= mon[mm]) {
st[mm][dd] = 1;
}
}
}
}
}
}
}
}
}
int ans = 0;
for (int i = 1; i <= 12; i++)
for (int j = 1; j <= mon[i]; j++) ans += st[i][j];
cout << ans << endl;
clock_t time = clock();
cout << time / CLOCKS_PER_SEC / 60<< endl;
return 0;
}
试题 B: 01 串的熵(5)
对公式化简,\(h(s) = -\sum_0^1{(t_i*p_i*log_2(p_i))}\)
直接枚举0的数量,判断是否合法即可
答案:11027421
点击查看代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int main() {
int n = 23333333;
double xs = 11625907.5798;
for (int i = 0; i <= n; i++) {
int t0 = i, t1 = n - i;
double p0 = 1.0 * t0 / n, p1 = 1 - p0;
double s0 = -t0 * p0 * log(p0) / log(2);
double s1 = -t1 * p1 * log(p1) / log(2);
if (abs(s0 + s1 - xs) <= 1e-4) {
cout << t0 << " " << t1 << endl;
}
}
return 0;
}