📄 lotto.cpp
字号:
//4.许多州的彩票发行机构都使用如程序清单7.4所示的简单彩票玩法的变体。
//在这些玩法中,玩家从一组被称为域号码(field number)的号码中选择几个。
//例如,可以从域号码1~47中选择5个号码:还可以从第二个间(如1~27)
//中选择一个号码(称为特选号码)。要赢得头等奖,必须正确猜中所有的号码。
//中头奖的几率是选中所有域号码几率与选中特选号码几率的乘积。请修改程序清单
//7.4,以计算中得这种彩票头奖的赃证。
//lotto.cpp--probility of winning
#include <iostream>
long double probability(unsigned numbers, unsigned picks);
int main()
{
using namespace std;
double total , choices; //域号码
double spe_total , spe_choices; //特别号码
cout<<"Enter the total number of choices on the game card and"<<endl;
cout<<"the number of picks allowed in this form:"<<endl;
cout<<"(total choices spe_total spe_choices)"<<endl;
while ( (cin>>total>>choices>>spe_total>>spe_choices)
&& choices<=total && spe_choices<=spe_total )
{
cout<<"You have one chance in ";
cout<<probability(total,choices) * probability(spe_total,spe_choices);
cout<<" of winning.\n";
cout<<"Next four numbers (q to quit): ";
}
cout<<"bye\n";
return 0;
}
// the following function calculates the probability of picking picks
// numbers correctly from numbers choices
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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -