IPA FE Subject A 2023 Question 11

A question IPA Japan published in 令和5年度 基本情報技術者試験 科目A 公開問題 問11, translated into English. ITPEC is based on the same exams.

In the following flowchart, in order to have the processing executed in the order

① → ② → ③ → ⑤ → ② → ③ → ④ → ② → ⑥

which of the following is the relationship between the initial values a and b that should be given to m and n in ①? Here, a and b are both positive integers.

Start①m ← an ← b②m : n≠③m : n<=>④m ← (m − n)⑤n ← (n − m)⑥Print thevalue of mEnd

Explanation

3a = 2b — follow the required order step by step and turn each branch taken into a condition on a and b.

  • •①: m = a, n = b
  • •② → ③ → ⑤: m ≠ n and m < n, so a < b; then n ← b − a
  • •② → ③ → ④: m > n, so a > b − a; then m ← a − (b − a) = 2a − b
  • •② → ⑥: now m = n, so 2a − b = b − a, that is 3a = 2b

With 3a = 2b both inequalities hold as well: a = 2b/3 < b and 2a = 4b/3 > b. For example, a = 2, b = 3: (2, 3) → (2, 1) → (1, 1), and 1 is printed.

Answer (d)

Why not others:

  • •(a) — with a = 2b, m > n at the first ③, so ④ runs first and gives m = b = n: the order is ① → ② → ③ → ④ → ② → ⑥
  • •(b) — with 2a = b, ⑤ gives n = b − a = a = m, so the loop ends after one pass: ① → ② → ③ → ⑤ → ② → ⑥
  • •(c) — with 2a = 3b, a > b, so ④ comes before ⑤: ① → ② → ③ → ④ → ② → ③ → ⑤ → ② → ⑥, the two subtractions in the reverse order

Key rule: This flowchart is Euclid's algorithm by subtraction (it prints the GCD); to find inputs for a given path, write each branch taken as an inequality or equation and solve them together.

Algorithm and Programming · AI-written, may contain errors

This question comes from the past questions IPA Japan publishes, translated into English by ITPEC Practice. ITPEC Practice is an independent study tool and is not affiliated with IPA or ITPEC. See the original paper (PDF) or Report an issue.