機械は残量fuelが0以下、又は処理数doneがlimit以上になると停止する。稼働1回につきfuelを2減らし、doneを1増やす。fuelとlimitは非負整数である。仕様を満たす[a]と[b]の組はどれか。
整数型: done ← 0
while ([a])
fuel ← fuel − 2
[b]
endwhile- a: fuel > 0 or done < limit / b: done ← done + 2
- a: fuel ≥ 0 and done < limit / b: done ← done + 1
- a: fuel > 0 and done < limit / b: done ← done + 1
- a: fuel > 0 and done < limit / b: done ← done + 2
- a: fuel > 0 or done < limit / b: done ← done + 1
- a: fuel > 0 and done ≤ limit / b: done ← done + 1
正解と解説を見る
解答と解説
正解:3. a: fuel > 0 and done < limit / b: done ← done + 1
停止条件は(fuel≤0) or (done≥limit)である。その否定を継続条件にすると(fuel>0) and (done<limit)になる。doneは稼働回数そのものなので1ずつ増やす。初めからfuel又はlimitが0なら一度も稼働しない点も、この条件で満たせる。 ほかの選択肢 「a: fuel > 0 or done < limit / b: done ← done + 2」…停止条件の否定と回数の加算の両方が仕様と異なる。 「a: fuel ≥ 0 and done < limit / b: done ← done + 1」…fuel=0のときも本体へ進み、停止仕様に反する。 「a: fuel > 0 and done < limit / b: done ← done + 2」…稼働1回を2回と数えるため、残量が十分でも回数上限に早く達する。 「a: fuel > 0 or done < limit / b: done ← done + 1」…一方の停止条件を満たしても、他方が真なら稼働し続けてしまう。 「a: fuel > 0 and done ≤ limit / b: done ← done + 1」…done=limitでもさらに稼働するため、上限を1回超える。
このページの閲覧は学習記録に保存されません。記録するには学習画面で回答してください。