“在所有控制路径上递归”实现阶乘函数时出错
对于课堂,我有一个作业:
编写一个 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)
您的阶乘函数不会终止于 1,它只是无限递归。
int factorial(int set_number)
{
if (set_number <= 1)
return 1;
return set_number * factorial(set_number - 1);
}
你的编码风格也很差,看起来很像C。不需要在 main 之后定义阶乘和组合,并且在顶部声明所有变量,没有混合声明和初始化?
另外,你的主函数实际上并没有按照规范所说的那样做——你从来没有初始化或分配给组合变量,也没有调用组合函数,你的变量命名得很糟糕,等等。但这是你的作业,不是我的。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
递归函数定义有两个关键元素:
您似乎缺少终止条件。
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?