考虑以下伪代码,其中 x 和 y 是正整数。
begin
q := 0
r := x
while r >= y do
begin
r := r – y
q := q + 1
end
end
程序终止后需要满足的后置条件为
(A) {r = qx + y ∧ r < y}
(B) {x = qy + r ∧ r < y}
(C) {y = qx + r ∧ 0 < r < y}
(D) { q + 1 0}答案:(乙)
说明:给定的伪代码对给定的 x 和 y 执行以下操作,它们是正整数。
1) It initializes r as x.
2) It repeatedly subtracts y from r until r becomes
smaller than y. For every subtraction, it
increments count q.
3) Finally r contains remainder, i.e., x%y and q contains
⌊x/y⌋
请参阅下面带有注释的伪代码。
begin
q := 0 // q is going to contain floor(x/y)
r := x // r is going to contain x % y
// Repeatedly subtract y from x.
while r >= y do
begin
r := r – y
q := q + 1
end
end
这个问题的测验