洛谷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:
}
}
Upd:2026/5/5/8:42

浙公网安备 33010602011771号