【问题标题】:How to exit recursion in ANSI C [closed]如何在 ANSI C 中退出递归 [关闭]
【发布时间】:2014-07-18 10:34:24
【问题描述】:

我正在编写一个简单的程序来计算 ANSI C 中的阶乘。该程序可以正常工作到 12,因为超过 13 的数字对于 INT 来说太大了。我想要做的是打印前 12 个,然后以某种方式 BREAK 并发送第 13 个数字的错误。但是,问题是当我到达第 13 个数字并发送错误时,前 12 个不打印。是否有可能返回错误并仍然获得前 12 个?

我的代码:

#include <math.h>
#include<stdio.h>

int getIntFactorial(int x){

printf("Processing factorial of (%d) \n", x);
if (x <=1){
    printf("\n Reached base case, returning %d \n", x);
    printf("Now returning total value \n");
    return 1;
}else if (x < 13){
    printf("\n Doing recursion by calling factorial (%d - 1) \n", x);

    int counter = x * getIntFactorial(x - 1);

    printf("Receiving results of factorial (%d) = %d * %d = %d \n", x, x, (x-1), counter);
    return counter;
}else {
///number is greater than 13
    printf("Sorry, we cannot do the factorial for %d, only goes up to 12 \n", x);
    return;
}



}


main()
{
    getIntFactorial(13);


}

13 的输出:

Processing factorial of (13)
Sorry, we cannot do the factorial for 13, only goes up to 12

Process returned 62 (0x3E)   execution time : 0.009 s
Press any key to continue.

12 的输出:

Processing factorial of (12)

 Doing recursion by calling factorial (12 - 1)
Processing factorial of (11)

 Doing recursion by calling factorial (11 - 1)
Processing factorial of (10)

 Doing recursion by calling factorial (10 - 1)
Processing factorial of (9)

 Doing recursion by calling factorial (9 - 1)
Processing factorial of (8)

 Doing recursion by calling factorial (8 - 1)
Processing factorial of (7)

 Doing recursion by calling factorial (7 - 1)
Processing factorial of (6)

 Doing recursion by calling factorial (6 - 1)
Processing factorial of (5)

 Doing recursion by calling factorial (5 - 1)
Processing factorial of (4)

 Doing recursion by calling factorial (4 - 1)
Processing factorial of (3)

 Doing recursion by calling factorial (3 - 1)
Processing factorial of (2)

 Doing recursion by calling factorial (2 - 1)
Processing factorial of (1)

 Reached base case, returning 1
Now returning total value
Receiving results of factorial (2) = 2 * 1 = 2
Receiving results of factorial (3) = 3 * 2 = 6
Receiving results of factorial (4) = 4 * 3 = 24
Receiving results of factorial (5) = 5 * 4 = 120
Receiving results of factorial (6) = 6 * 5 = 720
Receiving results of factorial (7) = 7 * 6 = 5040
Receiving results of factorial (8) = 8 * 7 = 40320
Receiving results of factorial (9) = 9 * 8 = 362880
Receiving results of factorial (10) = 10 * 9 = 3628800
Receiving results of factorial (11) = 11 * 10 = 39916800
Receiving results of factorial (12) = 12 * 11 = 479001600

Process returned 479001600 (0x1C8CFC00)   execution time : 0.015 s
Press any key to continue.

【问题讨论】:

  • 您的编译器应该抱怨您的函数的else { …; return; } 部分缺少返回值。你需要返回一些东西;也许0-1 会可用,或半可用。

标签: c recursion factorial


【解决方案1】:

您可以先测试一下您的参数是否大于 12,如果是,则打印您的错误消息,然后以 12 作为参数调用您的递归函数。

【讨论】:

  • 那是另一种可能性,我也在考虑,它会起作用。
【解决方案2】:

您在这里要求做什么并不完全清楚。很明显,鉴于您的代码,您观察到的行为是正确的:当然,当您尝试调用 getIntFactorial(13) 时,前十二个数字的阶乘不会打印出来。您的代码就是这样做的。

getIntFactorial 的伪代码结构是这样的:

if the number is less than or equal to 1:
    return 1;
if the number is less than 13:
    return the number times the factorial of the number minus one
if the number is greater than or equal to 13:
    print an error and return nothing

注意,顺便说一下,如果您的输入大于 13,您调用 return 而不返回任何内容。您的函数声称返回一个int,它是something。您返回的 nothingsomething 相比非常不同。你的编译器应该抱怨,而且应该大声抱怨。如果没有,你应该买一个新的。

【讨论】:

    【解决方案3】:

    根据我对需求的理解,将最后一个else 块中的return; 更改为return getIntFactorial(x-1) 会产生您想要的结果吗?

    【讨论】:

    • 这确实产生了我想要的结果,虽然没有我想象的那么漂亮,但它确实有效。
    • @user3386436 尝试获取 17 的阶乘,看看会产生什么结果。如果你想打印一个数字的所有阶乘,有一种更简单的方法,而不是你正在做的复杂的方式。
    • 它将打印“Can't do 17, 16 ,15”等,直到达到12,然后开始正常打印。是的,这就是我的想法,通过向后执行它是唯一可行的方法。我可能会让它看起来更好。
    猜你喜欢
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-27
    • 2020-01-14
    • 2020-12-06
    • 2012-02-11
    • 2013-08-27
    相关资源
    最近更新 更多