洛谷B4050[GESP202409 五级] 挑战怪物

题目传送门

一道水黄

思路

思路很简单,就是先用物理攻击,如果在中途出现 h 为质数的情况,就可以用魔法

攻击一击必杀,否则一直攻击到不能再攻击的时候。然后再看 h 是否为零,为零输

出攻击次数,否则输出-1.

代码

#include<bits/stdc++.h>
using namespace std;
int T;
bool is_p(int x){
    if(x < 2) return 0;
    for(int i = 2; i * i <= x; i++)
        if(x % i == 0) return 0;
    return 1;
}
int main(){
    cin >> T;
    while(T--){
        int h;
        cin >> h;
        int cnt = 0, i = 1;
        while(h > 0){
            if(is_p(h)){
                cout << cnt + 1 << endl;
                goto next;
            }
            h -= pow(2, i - 1);
            i++;
            cnt++;
        }
        if(h < 0) cout << -1 << endl;
        else if(h == 0) cout << cnt << endl;
        next:
    }
}

AC记录

Upd:2026/5/5/8:42

点个赞再走吧!!!!!

posted @ 2026-05-05 08:59  小汪同学^_^  阅读(7)  评论(1)    收藏  举报