大乐透模拟自选

28 篇文章 3 订阅
14 篇文章 1 订阅

模拟自选 

//模拟自选 
/*大乐透投注区分为前区号码和后区号码,前区号码范围为01~35,后区号码范围为01~12。
大乐透每期从35个前区号码中开出5个号码,
从12个后区号码中开出2个号码作为中奖号码,大乐透玩法即是竞猜开奖号码的5个前区号码和2个后区号码,顺序不限。
*/

#include <iostream>//c ++输入输出 
#include <stdlib.h>//含srand()选种子函数,rand随机函数 
#include <ctime>//time()函数 获取当前时间
#include <unistd.h>//延时函数 
#define Max1 35 //5个前区范围 1 ~ 35
#define Max2 12 //2个后区范围 1 ~ 12 
using namespace std;

//判断是否重号(前区不能重号,后区不能重号),前区可能与后区有同号 
int judge(int a[],int x)
{
	if(a[0] == 0)
	{
		return 0;
	}
	
	int i = 0;
	for(i = 0;a[i] != 0;i ++)
	{
		if(a[i] == x)//判断是否重号
		{
			return 1;//是 
		}
	}

	return 0;
}//有重号 返回 1,没有重号返回0 

int main()
{
	int count = 1,x = 0,rand1,rand2;
	rand1 = rand2 = 1;
	while(1)
	{
		int front[5] = {0},back[2] = {0};//每次初始化 前区,后区数组 
		cout << "第" << count <<"次生成号码:";
    	//前区 
		srand((unsigned)time(NULL));//以当前时间为种子(可以不用unsigned)
    	for(int i = 0;i < 5;)//5 个前区 
    	{

    		rand1 = rand() % Max1 + 1;
    		if(judge(front,rand1) == 1)//有重号 
			{
				continue;//舍去该号码重新选 
			}
			front[i] = rand1;
    		cout << rand1 << " ";//rand() 内部实现是用线性同余法做的
    		sleep(1);
    		
    		i ++;//选下一个号码 
		}
		//后区 
	    srand((unsigned)time(NULL));//以当前时间为种子(可以不用unsigned)
		for(int i = 0;i < 2;)//2个后区
		{
			rand2 = rand() % Max2 + 1;
			if(judge(back,rand2) == 1)//有重号 
			{
				continue;//舍去该号码重新选 
			}
			back[i] = rand2;
			cout << rand2 << " ";
			sleep(1);
			
			i ++;//选下一个号码 
		}
	    	
	    cout << endl;
	    
	    count ++;
	    
		cout << "退出输入2,继续请输入1: ";
		cin >> x;
		if(x == 2)
		{
			break;
		}
		else;
		
		cout << endl;
	}
    
	return 0;
}
/*
有时程序需要一个特定范围内的随机数。要将随机数的范围限制在 1 和某个最大值 max 之间的整数,可以使用以下公式:
number = rand() % Max + 1;
*/

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值