我必须做一个代码,计算中奖彩票的概率,给定的数量可供选择的数量以及您必须选择多少。我必须在代码中使用阶乘方程(n!)/(k!*(n-k)!)。代码本身工作正常,但公式不会编译。概率计算器与阶乘方程
//This program calculates the probability of winning the lottery
#include
using namespace std;
double factorial(int n, int k);
int main()
{
//variables
int n;
int k;
char pa;
int chance;
double prob;
//loop
do
{
cout << "How many numbers (1-12) are there to pick from?\n" << endl;
cin >> n;
if(n>12 || n<1)
{
cout << "Invalid entry.\nHow many numbers (1-12) are there to pick from?\n";
cin >> n;
}
cout << "How many numbers must you pick to play?\n";
cin >> k;
if(k>n || k<1)
{
cout << "Invalid entry.\nHow many numbers must you pick to play?\n";
cin >> n;
}
cout << "Your chance of winning the lottery is 1 in " << chance << endl;
prob=factorial(n, k);
cout << "This is a probability of " << prob << endl;
cout << "Play again?";
cin >> pa;
} while (pa != 'n');
return 0;
}
double factorial(int n, int k)
{
double fact;
fact=(n!)/(k!*(n-k)!);
return fact;
}
+0
即使您的代码确实有效,您的函数“factorial”甚至不计算该因子。写一个计算n的函数阶乘!并在另一个函数内使用它来计算概率。你在'factorial'函数中的公式会计算一个二项式系数(如果编译的话) –
+0
非C代码...去除标签。 –