“在所有控制路径上递归”实现阶乘函数时出错
对于课堂,我有一个作业:
编写一个 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 ofn
objects (bothn
andk
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 returnn!
. The second function should be calledcombinations
and should returnn!/(k! * (n - k)!).
Test your program for different values ofn
andk
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
递归函数定义有两个关键元素:
您似乎缺少终止条件。
factorial()
如何永远停止调用自身?There are two critical elements to a recursive function definition:
You appear to be missing the termination condition. How would
factorial()
quit calling itself forever?您定义了一个递归函数(即基本上是一个调用自身的函数),但尚未定义退出条件。您在返回之前再次调用 Factorial ,因此该函数将永远不会结束,一遍又一遍地调用自身。
你需要在那里添加一个分支,即
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.
您缺少一个基本案例。对于 set_number <= 1,阶乘应返回 1
You are missing a base case. Factorial should return 1 for set_number <= 1
该函数将导致不定式递归,因为它永远不会停止调用自身:
这就是您想要的:
This function will result in infinitive recursion because it never stops calling itself:
This is what you want:
您的阶乘函数不会终止于 1,它只是无限递归。
你的编码风格也很差,看起来很像C。不需要在 main 之后定义阶乘和组合,并且在顶部声明所有变量,没有混合声明和初始化?
另外,你的主函数实际上并没有按照规范所说的那样做——你从来没有初始化或分配给组合变量,也没有调用组合函数,你的变量命名得很糟糕,等等。但这是你的作业,不是我的。
Your factorial function doesn't terminate on one, it just recurses indefinitely.
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.