本文实例讲述了C++基于随机数实现福彩双色球的方法。分享给大家供大家参考,具体如下:
这是前段时间写的福彩双色球一个小应用
本来可以一个文件搞定,反正也没多大,就分开了.
头文件doubleColorBallR2.h
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#ifndef _DoubleColorBallR2_h
#define _DoubleColorBallR2_h
#include <iostream>
#include <stdio.h>
#include <vector>
#include <list>
#include "windows.h"
#include <algorithm>
#ifndef _RED_ZONE_
#define _RED_ZONE_ 33
#endif
#ifndef _BLUE_ZONE_
#define _BLUE_ZONE_ 16
#endif
#ifndef _RED_NUM_
#define _RED_NUM_ 6
#endif
#ifndef _BLUE_NUM_
#define _BLUE_NUM_ 1
#endif
using namespace std;
class DoubleColorBallR2
{
public :
DoubleColorBallR2(){
}
~DoubleColorBallR2(){
}
void getRedZone();
void getBlueZone();
private :
};
#endif
|
实现文件 doubleColorBallR2.cpp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include "doubleColorBallR2.h"
void DoubleColorBallR2::getRedZone(){
vector< int > v_red; //选择数的范围 1-_RED_ZONE_
list< int > l_red; //装入1-_RED_NUM_个数
for ( int i=1; i <= _RED_ZONE_; ++i) {
v_red.push_back(i);
}
srand ((unsigned)GetCurrentTime());
int j=_RED_ZONE_;
for ( int i=1; i<=_RED_NUM_; ++i) {
int n=1+ rand ()%(j-1+1);
l_red.push_back(v_red[n]); //装入
//删除v_red已装入的数
vector< int >::iterator iter=find(v_red.begin(), v_red.end(), v_red[n]);
v_red.erase(iter);
--j; //由于v_red已经删除了一位数,所以随机数取值范围要减少一位
}
l_red.sort();
for (list< int >::iterator i=l_red.begin(); i!=l_red.end(); ++i) {
cout<<*i<< " " ;
}
}
void DoubleColorBallR2::getBlueZone(){
vector< int > v_blue; //选择数的范围 1-_BLUE_ZONE_
list< int > l_blue; //装入1-_BLUE_NUM_个数
for ( int i=1; i <= _BLUE_ZONE_; ++i) {
v_blue.push_back(i);
}
srand ((unsigned)GetCurrentTime());
int j=_BLUE_ZONE_;
for ( int i=1; i<=_BLUE_NUM_; ++i) {
int n=1+ rand ()%(j-1+1);
l_blue.push_back(v_blue[n]); //装入
//删除v_red已装入的数
vector< int >::iterator iter=find(v_blue.begin(), v_blue.end(), v_blue[n]);
v_blue.erase(iter);
--j; //由于v_red已经删除了一位数,所以随机数取值范围要减少一位
}
l_blue.sort();
for (list< int >::iterator i=l_blue.begin(); i!=l_blue.end(); ++i) {
cout<<*i<< " " ;
}
}
|
主程序 doubleColorBall.cpp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream>
#include <list>
#include "windows.h"
#include <stdio.h>
#include "doubleColorBallR2.h"
#define RED_ZONE 33 //红区
#define BLUE_ZONE 16 //蓝区
#define RED_NUM 6 //红区位数
#define BLUE_NUM 1 //蓝区位数
using namespace std;
int main ( int argc, char *argv[])
{
DoubleColorBallR2 dcb;
dcb.getRedZone();
dcb.getBlueZone();
getchar ();
return (0);
}
|
相关文章
猜你喜欢
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 102
-
2025-05-29 75
-
2025-05-27 42
-
2025-05-27 103
-
2025-05-25 88
热门评论