[理工] 演算法-遞迴

作者: rex51920594 (rex51920594)   2018-08-14 01:36:14
The following recursive program segment is written in C language.
Write the output if it is called with the instruction xbox (3, ‘A’, ‘B’,
‘C’).
void xbox (int n, char x, char y, char z)
{
if (n > 0 ) {
printf(“%c %c %c \n”, x, y, z);
xbox (n-1 , x, z, y);
xbox (n-1 , y, x, z);
}
}
解:
n = 3 A B C
n = 2 A C B
n = 1 A B C
n = 1 C A B
n = 2 B A C
n =1 B C A
n =1 A B C
作者: seika555 (kakkoii)   2018-08-14 02:13:00
" target="_blank" rel="nofollow">
一圈一圈遞迴下去想比較好想
作者: hacocu831735 (pppx)   2018-08-14 04:16:00
請問為什麼n=0時不會繼續遞迴?不就只是不會印出東西而已嗎?
作者: goldstorm (哦齁哦齁)   2018-08-14 09:54:00
n=0不會進入if (n > 0)的條件裡
作者: seika555 (kakkoii)   2018-08-14 09:57:00
因為不會進到if裡面 所以不會印出東西 就不會再遞迴下去了吧
作者: hacocu831735 (pppx)   2018-08-14 10:09:00
感謝…沒看清楚括號

Links booklink

Contact Us: admin [ a t ] ucptt.com