[理工] 算法-递回

楼主: 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
https://imgur.com/UzzdWcv.jpg一圈一圈递回下去想比较好想
作者: 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