1、算彩票的概率时,一般是几千万分之一,但结果表示的时候不要表示为1/(A),这样计算机会显示为0。应该表示为A,one chance in A。
2、算C27,4类似问题时,不应这样表示[(A)*(B)*(C)*(D)]/[(a)*(b)*(c)*(d)],可能会数字溢出,超出限制。应表示成(A/a)*(B/b)*(C/c)*(D/d),可有效防止大数字。
#include <iostream>
using namespace std;
long double probablity (unsigned int nomal_numbers,unsigned int npicks,
unsigned int special_numbers,unsigned int spicks);
int main()
{
unsigned nn,np,sn,sp;
cout<<"Enter nomal number, npicks,sepcial number,spicks:\n";
while((cin>>nn>>np>>sn>>sp)&&np<=nn&sp<=sn)
{
cout<<"You have \"one\" chance in " //~~~~~~~~~~~~~~
<<probablity(nn,np,sn,sp)<<endl;
cout<<"Next four numbers is(q to quit):";
}
cout<<"BYE!";
system("pause");
return 0;
}
long double probablity (unsigned int nomal_numbers,unsigned int npicks,
unsigned int special_numbers,unsigned int spicks)
{
long double chance1=1.0,chance2=1.0;
for ( npicks,nomal_numbers ;npicks>0;nomal_numbers--,npicks--)
chance1 =chance1*nomal_numbers/npicks; //~~~~~~~~~~~~~~~~
for (spicks ,special_numbers ;spicks>0;spicks--,special_numbers--)
chance2 =chance2*special_numbers/spicks;
return chance1*chance2;
}