次の手続によって得られる結果と同じ結果が得られる命令の記述はどれか。
〔手続〕
(1) 件数表示(価格=1)を実行し,表示される結果を w とする。
(2) 件数表示([性能=1] and [価格=1])を実行し,表示される結果を x とする。
(3) 件数表示([価格=1] and [デザイン=1])を実行し,表示される結果を y とする。
(4) 件数表示([性能=1] and [価格=1] and [デザイン=1])を実行し,表示される結果を z とする。
(5) w-x-y+z を計算する。
出典 平成27年度 春期 ITパスポート試験 問100
解説
問 100
//
○ア
正解Let P be the set with Performance=1, K the set with Price=1, and D the set with Design=1. Then w=|K|, x=|P∩K|, y=|K∩D|, and z=|P∩K∩D|. Subtracting x and y from w removes from K the part that belongs to P and the part that belongs to D, but the overlap that belongs to both P and D (P∩K∩D) gets subtracted twice, so z is added back as a correction. The result w-x-y+z = |K|-|K∩P|-|K∩D|+|K∩P∩D| represents the number of records with Price=1 that are neither Performance=1 nor Design=1 — that is, the count of the part of K belonging to neither P nor D. Written as a single command this is Count display ([Performance=0] and [Price=1] and [Design=0]), so ア is correct.
×Incorrectイ
In “[Performance=0] or [Price=1] or [Design=0]” all three conditions are joined by or, so a record is counted whenever even one of them holds. A record with Price=0, for instance, is still counted as long as Performance=0, making the set far wider than the “Price=1 and Performance=0 and Design=0” that the procedure asks for. Note also that, by De Morgan's laws, this expression is the negation of the condition in ウ.
×Incorrectウ
Every item value here is the exact opposite of ア: the expression counts records with Performance=1, Price=0 and Design=1. The procedure starts from w=|K|, the count of records with Price=1, and no matter what is added or subtracted afterwards the target stays limited to records with Price=1, so it cannot match an expression whose condition requires Price=0.
×Incorrectエ
Negating the condition of ア, “[Performance=0] and [Price=1] and [Design=0]”, by De Morgan's laws swaps and with or and flips each condition, giving “[Performance=1] or [Price=0] or [Design=1]”. In other words エ counts the complement of ア, so its count equals the total number of records minus the result of the procedure (w-x-y+z). That is exactly the reverse of what is being asked for, so it does not match.
ここがポイント
Inclusion-exclusion principle: subtract |K∩P| and |K∩D| from |K|, then add back |K∩P∩D|, which was subtracted twice, and you obtain |K∩P^c∩D^c|. It is easier to picture with a Venn diagram, erasing from the K region the parts that overlap P and D.
De Morgan's laws: negating an entire condition expression swaps and with or and negates each individual condition. Remembering that the negation of [Performance=0] and [Price=1] and [Design=0] is [Performance=1] or [Price=0] or [Design=1] lets you quickly tell apart the and-type and or-type choices, which come in negation pairs.