题解:P12170 [蓝桥杯 2025 省 Python B] 攻击次数

· · 题解

本题考查分支语句。

在这里,使用 ans 记录回合数,n 记录血量。

第一个英雄恒定,直接减去 5 即可。

第二个英雄奇数减 15,偶数减 2。

if(ans%2==1) n-=15;
else n-=2;

第三个英雄当回合数除以 3 的余数为 1 时攻击 2 的血量;当回合数除以 3 的余数为 2 时攻击 10 的血量;当回合数除以 3 的余数为 0 时攻击 7 的血量。

if(ans%3==1) n-=2;
else if(ans%3==2) n-=10;
else n-=7;

完整代码:

#include<bits/stdc++.h>
using namespace std;
int ans=0;
int main(){
    int n=2025;
    while(1){
        ans++;
        n-=5;
        if(ans%2==1) n-=15;
        else n-=2;
        if(ans%3==1) n-=2;
        else if(ans%3==2) n-=10;
        else n-=7;
        if(n<=0) cout<<ans,exit(0);
    }
    return 0;
}