#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include<vector>
#include<algorithm>
using namespace std
int obj[7]
int select[7]
vector<int>bingo,b1
vector<int>::iterator it
int rnd(int s[7],vector<int>&b)
{
int i,j
for( i = 0i <7i++ )
{
s[i]=rand()%33+1//取1-33的随机数
}
for(i=0i<7i++)
{
for(j=i+1j<7j++)
{
if (s[i]==s[j]) //判断产生的随机数是否有重复
return 1//如发生重复返回1
}
}
for(i=0i<7i++)
b.push_back (s[i])//进栈
sort(b.begin (),b.end())//对产生随机数排序
return 0
}
int trap()//用于手选号码的错误判定
{
for(int i=0i<7i++)
{
for(int j=i+1j<7j++)
{
if (b1[i]==b1[j])//判定是否有重复
return 1
}
if((b1[i]<1)||(b1[i]>33))//判定是否超出数据范围
return 1
}
return 0
}
int hand()//手选号码
{
for(int i=0i<7i++)
{
cout<<"请输入第"<<i+1<<"个号码:"
cin>>select[i]
b1.push_back(select[i])
cout<<endl
}
if (trap()==1)
{
cout<<"您输的号码有重复或超出了1-33的范围,请重新输入."<<endl
b1.clear ()//清空错误数据
hand()//递归,如号码出错重选
}
for (i=0i<7i++)
{
cout<<b1[i]<<"\t"//号码正确则输出
}
cout<<endl
return 0
}
int god()//机选号码
{
int a
cout<<"您的机选号码是:..."<<endl
do
{
a=rnd(select,b1)
if(a==0)
for(int i=0i<b1.size()i++)
{
cout<<b1[i]<<"\t"
}
}
while(a==1)//错误控制
cout<<endl
return 0
}
int lucky()//产生中奖号码
{
int b
cout<<"中奖号码是:..."<<endl
do
{
b=rnd(obj,bingo)
if(b==0)
for(int i=0i<bingo.size()i++)
{
cout<<bingo[i]<<"\t"
}
}
while(b==1)
return 0
}
int check()//检查中奖情况
{
int count=0//猜中号码个数
for(int i=0i<7i++)
{
it=find(bingo.begin(),bingo.end(),b1[i])
if(it!=bingo.end())
{
count++
}
}
return count
}
int result(int p)//概率统计
{
int c[8]={0,0,0,0,0,0,0,0}
int yun
lucky()
for(int k=0k<pk++)
{
god()
c[check()]++//将猜中每个号码个数的次数存入该数组相应的下标元素
b1.clear ()
}
cout<<endl<<"在上述测试中:"<<endl
for(int j=0j<8j++)
{
yun=c[j]*100/p
cout<<"猜中"<<j<<"个号码的次数为"<<c[j]<<"次"<<endl
cout<<"猜中"<<j<<"个号码的概率为"<<yun<<"%"<<endl
}
bingo.clear ()
return 0
}
int main()
{
char l
int t=0
srand( (unsigned)time( NULL ) )//随机种子
do
{
cout<<"****************************************************"<<endl
cout<<"* 全民大彩票,惊喜多多,中奖多多,欢迎大家踊跃购买啦! *"<<endl
cout<<"* 压得多,赢得多,回家买辆大卡车!*"<<endl
cout<<"*开心刺激33选7,马上就要开始了,赶快押宝了 *"<<endl
cout<<"****************************************************"<<endl
cout<<"[1]手选号码"<<endl
cout<<"[2]机选号码"<<endl
cout<<"[3]概率统计"<<endl
cout<<"[4]退出"<<endl
cout<<"请输入您的选择[1-3]"
cin>>l
switch(l)
{
case '1':
hand()
lucky()
cout<<endl<<"您猜中了【"<<check()<<"】个号码"<<endl
bingo.clear ()
b1.clear ()
break
case '2':
god()
lucky()
cout<<endl<<"您猜中了【"<<check()<<"】个号码"<<endl
bingo.clear ()
b1.clear ()
break
case '3':
cout<<"请输入测试的次数:"
cin>>t
result(t)
break
case '4':
cout<<"十赌九输啊!快走吧,再晚了就改不上2路汽车了..."<<endl
break
default:
cout<<"请输入1-3之间的数字"<<endl
}
}
while(l!='4')
return 0
}
我以前用过的一个程序 你试试
Dim a(6) As Integer '声明数组Function JC(n As Integer) As Double
'求阶乘
JC = 1
For i = 1 To n
JC = JC * i
Next
End Function
Sub S36_7()
'选号
On Error Resume Next
For i = 0 To 6
Line1:
a(i) = Fix(Rnd * 36) + 1
For j = 0 To i - 1
If a(i) = a(j) Then
GoTo Line1
End If
Next
Next
End Sub
Sub PrintNum()
'输出
For i = 0 To 6
Print a(i)
Next
End Sub
Private Sub Command1_Click()
'调用
Call S36_7
Call PrintNum
End Sub
Private Sub Command2_Click()
'输出
Print "获奖概率"JC(36) / JC(7) / JC(36 - 7)
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)