What is the output result of the following program?
#include <stdio.h>
void fA(int t) {
printf("%i ", t);
if (t < 3) fA(t + 1);
else printf("%i ", t);
printf("%i ", t);
}
main() {
fA(0);
}
A0 1 2 2 2 1 0
B1 2 2 2 1
C0 1 2 3 3 3 2 1 0正確答案
D1 2 3 3 3 2 1
What is the output result of the following program?
答案與詳解
C
正確答案
本題因排版誤植混入兩段程式與兩組選項,官方答案 C 對應第一段遞迴程式之輸出,第二段 while 迴圈實際輸出為 5 6 7 7 5 3。
逐選項解析
A0 1 2 2 2 1 0✕ 錯誤
遞迴展開時,fA(3) 會印出三次 3,而非兩次 2,故輸出錯誤。
B1 2 2 2 1✕ 錯誤
未包含初始呼叫 fA(0) 與 fA(3) 的完整印出結果,邏輯錯誤。
C0 1 2 3 3 3 2 1 0✓ 正確
D1 2 3 3 3 2 1
What is the output result of the following program?✕ 易錯陷阱