“在所有控制路径上递归”实现阶乘函数时出错

发布于 2024-09-27 08:41:37 字数 1765 浏览 2 评论 0原文

对于课堂,我有一个作业:

编写一个 C++ 程序,该程序将输出多种不同方式,您可以通过这些方式从一组 n 对象(均为 n< /code> 和 k 应该是正整数)。该数字由以下公式给出:

C(n, k) = n!/(k! * (n - k)!)

您的程序应该使用两个返回值的函数。第一个应该被称为factorial并且应该返回n!。第二个函数应称为 combinations 并应返回 n!/(k! * (n - k)!)。 测试您的程序是否有不同的 n 值k 五次(计数控制循环)。

我想出了一个解决方案:

#include <iostream>
using namespace std;
int factorial(int);
int combination(int, int);

void main(void)
{
    int objects, set_number, count; 
    count = 1; 
        while(count <= 5)
        {
            cout << "Please enter in number of objects ";
            cin >> objects; 
            cout << "Please enter in the number of Sets ";
            cin >> set_number;
            count++;
        }

    cout << "The Factorial is " << factorial(set_number) << " & the combination is " << combination << endl;
    cout << endl; 
}

// Factorial 
int factorial(int set_number)
{
    int cal;
    cal = set_number * factorial(set_number - 1);
    return cal; 
}

//  Combination
int combination(int objects, int set_number)
{
    int com_total, cal_set, cal_obj, min_sum, cal_min;

    cal_set = set_number * factorial(set_number - 1);
    cal_obj = objects * factorial(objects - 1);
    
    //n!/(k! * (n - k)!)
    min_sum = set_number - objects; 
    cal_min = min_sum * factorial(min_sum- 1);
    com_total = cal_set / (cal_obj * cal_min);
    return com_total; 
}

……但我不断收到错误消息;

“'阶乘':在所有控制路径上递归,函数会导致运行时堆栈溢出;”

如果有人可以帮助我,我已经为此工作了大约一个小时,但我被难住了!

For class I have an assignment:

Write a C++ program that will output the number of distinct ways in which you can pick k objects out of a set of n objects (both n and k should be positive integers). This number is given by the following formula:

C(n, k) = n!/(k! * (n - k)!)

Your program should use two value-returning functions. The first one should be called factorial and should return n!. The second function should be called combinations and should return n!/(k! * (n - k)!). Test your program for different values of n and k five times (count-controlled loop).

I came up with a solution:

#include <iostream>
using namespace std;
int factorial(int);
int combination(int, int);

void main(void)
{
    int objects, set_number, count; 
    count = 1; 
        while(count <= 5)
        {
            cout << "Please enter in number of objects ";
            cin >> objects; 
            cout << "Please enter in the number of Sets ";
            cin >> set_number;
            count++;
        }

    cout << "The Factorial is " << factorial(set_number) << " & the combination is " << combination << endl;
    cout << endl; 
}

// Factorial 
int factorial(int set_number)
{
    int cal;
    cal = set_number * factorial(set_number - 1);
    return cal; 
}

//  Combination
int combination(int objects, int set_number)
{
    int com_total, cal_set, cal_obj, min_sum, cal_min;

    cal_set = set_number * factorial(set_number - 1);
    cal_obj = objects * factorial(objects - 1);
    
    //n!/(k! * (n - k)!)
    min_sum = set_number - objects; 
    cal_min = min_sum * factorial(min_sum- 1);
    com_total = cal_set / (cal_obj * cal_min);
    return com_total; 
}

...but I keep getting an error, that says;

"'factorial' : recursive on all control paths, function will cause runtime stack overflow;"

If someone could help me, I've been working on this for about an hour and I'm stumped!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

醉梦枕江山 2024-10-04 08:41:37

递归函数定义有两个关键元素:

  • 对自身的递归调用
  • 终止条件

您似乎缺少终止条件。 factorial() 如何永远停止调用自身?

There are two critical elements to a recursive function definition:

  • a recursive call to itself
  • a termination condition

You appear to be missing the termination condition. How would factorial() quit calling itself forever?

空袭的梦i 2024-10-04 08:41:37

您定义了一个递归函数(即基本上是一个调用自身的函数),但尚未定义退出条件。您在返回之前再次调用 Factorial ,因此该函数将永远不会结束,一遍又一遍地调用自身。

你需要在那里添加一个分支,即

if (set_number == 0)
{
   return 1;
}
else
   return set_number * factorial(set_number - 1);

You defined a recursive function (i.e. basically a function that calls itself), but you have not defined an exit condition. You are calling factorial again right before the return, so the function will never end, calling itself over and over again.

You need to add a branch in there, i.e.

if (set_number == 0)
{
   return 1;
}
else
   return set_number * factorial(set_number - 1);
无声情话 2024-10-04 08:41:37

您缺少一个基本案例。对于 set_number <= 1,阶乘应返回 1

You are missing a base case. Factorial should return 1 for set_number <= 1

苍暮颜 2024-10-04 08:41:37

该函数将导致不定式递归,因为它永远不会停止调用自身:

int factorial(int set_number)
{
    int cal;
    cal = set_number * factorial(set_number - 1);
    return cal; 
}

这就是您想要的:

int factorial(int n)
 {
  if (n<=1)
    return(1);
  else
    n=n*factorial(n-1);
    return(n);
 }

This function will result in infinitive recursion because it never stops calling itself:

int factorial(int set_number)
{
    int cal;
    cal = set_number * factorial(set_number - 1);
    return cal; 
}

This is what you want:

int factorial(int n)
 {
  if (n<=1)
    return(1);
  else
    n=n*factorial(n-1);
    return(n);
 }
执笔绘流年 2024-10-04 08:41:37
int factorial(int set_number)
{   
   return set_number == 1?1:set_number * factorial(set_number - 1);
}
int factorial(int set_number)
{   
   return set_number == 1?1:set_number * factorial(set_number - 1);
}
揽清风入怀 2024-10-04 08:41:37

您的阶乘函数不会终止于 1,它只是无限递归。

int factorial(int set_number)
{
    if (set_number <= 1)
        return 1;
    return set_number * factorial(set_number - 1);
}

你的编码风格也很差,看起来很像C。不需要在 main 之后定义阶乘和组合,并且在顶部声明所有变量,没有混合声明和初始化?

另外,你的主函数实际上并没有按照规范所说的那样做——你从来没有初始化或分配给组合变量,也没有调用组合函数,你的变量命名得很糟糕,等等。但这是你的作业,不是我的。

Your factorial function doesn't terminate on one, it just recurses indefinitely.

int factorial(int set_number)
{
    if (set_number <= 1)
        return 1;
    return set_number * factorial(set_number - 1);
}

Your coding style is also pretty poor, it looks very C-like. There's no need for factorial and combination to be defined after main, and you declare all your vars at the top, no declarations and initializations mixed?

Also, your main function doesn't actually do what the specification says it should - you never initialized or assigned to the combinations variable nor called the combination function, your variables are terribly named, etc. But this is your homework, not mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文