Re: [理工] 算法-递回

楼主: Honor1984 (希望愿望成真)   2018-08-14 02:16:31
※ 引述《rex51920594 (rex51920594)》之铭言:
: 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)
称xbox
: {
: if (n > 0 ) {
: printf(“%c %c %c \n”, x, y, z);
: xbox (n-1 , x, z, y);
称xbox_1
: xbox (n-1 , y, x, z);
称xbox_2
: }
: }
以上那种称呼只是方便你了解
: 解:
: n = 3 A B C
xbox(3, 'A', 'B', 'C')中的printf
: n = 2 A C B
xbox_1(3-1, 'A', 'C', 'B')中的printf
=xbox(2, 'A', 'C', 'B')中的printf
: n = 1 A B C
xbox_1(2-1, 'A', 'B', 'C')中的printf
=xbox(1, 'A', 'B', 'C')中的printf
: n = 1 C A B
xbox_2(2-1, 'C', 'A', 'B')中的printf
=xbox(1, 'C', 'A', 'B')中的printf
: n = 2 B A C
xbox_2(3-1, 'B', 'A', 'C')中的printf
=xbox(2, 'B', 'A', 'C')中的printf
: n =1 B C A
xbox_1(2-1, 'B', 'C', 'A')中的printf
=xbox(1, 'B', 'C', 'A')中的printf
: n =1 A B C
xbox_2(2-1, 'A', 'B', 'C')中的printf
=xbox(1, 'A', 'B', 'C')中的printf
程式中又没有要打印n
回答怎么能把n = x写出来?
我要是阅卷者,第一个就给你打叉
:
作者: rex51920594 (rex51920594)   2018-08-14 20:13:00
感谢!答题会记住这点的

Links booklink

Contact Us: admin [ a t ] ucptt.com