自己做的一个关于彩票中奖率的计算代码
#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
double total1, choices, total2;
double probability1, probability2;
cout<<"Enter the total number of choices on the game card and\n"//输入第一个选区
"the number of picks allowed:\n";
while ((cin>>total1>>choices)&&choices<=total1)
{
cout<<"Enter total number of game card choices for the mege number:\n";//第二个..
if (!(cin>>total2))
break;
probability1=probability(total1, choices);
probability2=probability(total2, 1);
cout<<"You have one chance in "<<probability1*probability2<<"of winning.\n";
cout<<"Enter the total number of choices on the game card and\n"
"the number of picks allowed again:\n";
}
cout<<"Bye.\n";
return 0;
}
long double probability(unsigned numbers, unsigned picks)//计算中奖率
{
long double result=1.0;
long double n;
unsigned p;
for (n = numbers, p = picks; p>0; n--,p--)
result = result*n/p;
return result;
}