a456簡單迴圈模擬・同時成立的條件
APCS 2025/06:小心陷阱 (ZeroJudge q836)
依照跳躍、檢查落點、扣血的順序更新角色狀態。
這題在學什麼
用 position 和 health 記錄角色狀態。每回合先 position+=health,接著以 position%x1==0 和 position%x2==0 判斷兩種陷阱,分別扣除傷害。
只要 health>0 就繼續。兩個陷阱要用兩個獨立 if,因為同一落點可能同時觸發兩種傷害。每次生命下降後,後續跳躍距離也跟著改變。
Step-by-step animation
k=7 的角色狀態
步驟 1 / 3
第一跳
以生命 7 跳到位置 7,7 不是 3 或 2 的倍數,生命仍為 7。
0
7
14
18
- position
- 7
- health
- 7
#include <bits/stdc++.h>
using namespace std;
int main() {
int health, x1, y1, x2, y2;
cin >> health >> x1 >> y1 >> x2 >> y2;
int position = 0;
while (health > 0) {
position += health;
if (position % x1 == 0) health -= y1;
if (position % x2 == 0) health -= y2;
}
cout << position << '\n';
}先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。