蓝桥杯一周突破---day2【枚举】

张开发
2026/4/6 21:50:44 15 分钟阅读

分享文章

蓝桥杯一周突破---day2【枚举】
#includebits/stdc.h using namespace std; int Good(int x) { int pos 1; // 记录当前是第几位个位是1 while (x 0) { int digit x % 10; if (pos % 2 ! 0) { // 第1, 3, 5位个、百、万 if (digit % 2 0) return 0; // 应该是奇数 } else { // 第2, 4, 6位十、千 if (digit % 2 ! 0) return 0; // 应该是偶数 } x / 10; pos; } return 1; } int main(){ int n,cnt0; cinn; for(int i1;in;i){ if(Good(i)1) cnt; } coutcntendl; return 0; }P10424 [蓝桥杯 2024 省 B] 好数 - 洛谷D-小红杀怪_2024BistuACM萌新练习赛01如果像我一样纯模拟就会始终卡在最后一个测试点中#includebits/stdc.h using namespace std; int main(){ int a,b,x,y; cinabxy; int time0; if(xy*2){ time(ax-1)/x(bx-1)/x;//这里是向上取整的代码书写 } else{ while(a0b0){ a-y; b-y; time; } if(a0) { while(a0){ if(xy) { a-y; time; } else{ a-x; time; } } } if(b0) { while(b0){ if(xy) { b-y; time; } else{ b-x; time; } } } } couttimeendl; return 0; }正解应该是枚举群体技能设k法#include bits/stdc.h using namespace std; int main() { int a, b, x, y; if (!(cin a b x y)) return 0; int min_time 1000000; // 初始设一个很大的数用来寻找最小值 // 枚举使用群体技能 y 的次数因为血量最多 20最多也就用 20 次 for (int k 0; k 20; k) { // 1. 如果放了 k 次群体技能计算两只怪还剩多少血不能扣成负数最少是 0 int rem_a max(0, a - k * y); int rem_b max(0, b - k * y); // 2. 剩下的血量全部用单体技能 x 来补刀向上取整公式 int time_a (rem_a x - 1) / x; int time_b (rem_b x - 1) / x; // 3. 计算这种战术的总次数y 的次数 补刀 A 的次数 补刀 B 的次数 int total k time_a time_b; // 4. 更新最少次数 min_time min(min_time, total); } cout min_time \n; return 0; }P10387 [蓝桥杯 2024 省 A] 训练士兵 - 洛谷想不到了看大佬吧建议标签贪心。 有一个显然的贪心思路如果当前所有仍需要继续训练的士兵一次训练所需的金币总和小于 S就使用组团训练否则剩下的士兵全部单独训练。 输入时记 cnt i ​ 为需要训练 i 次的士兵一次训练所需的金币之和now 为所有士兵一次训练所需的金币之和Sum 为所有士兵单独训练所需的金币之和。 枚举组合训练的次数记 ans 为组合训练所需的金币之和。 对于第 i 次组合训练 如果 now≥S则说明还需要组合训练ans 加上 SSum 减去 nownow 减去 cnt i ​ 。 如果 nowS跳出循环答案即为 ansSum。 #include bits/stdc.h using namespace std; typedef long long LL; const int N 1e6 10; LL n, s, p[N], c[N], cnt[N], Sum, now, ans; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin n s; for (int i 1; i n; i) cin p[i] c[i], cnt[c[i]] p[i], now p[i], Sum p[i] * c[i]; for (int i 1; i 1e6; i) { if (now s) break; ans s; Sum - now; now - cnt[i]; } cout ans Sum; return 0; }

更多文章