溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++基于隨機數(shù)如何實現(xiàn)福彩雙色球

發(fā)布時間:2021-08-06 14:17:28 來源:億速云 閱讀:154 作者:小新 欄目:編程語言

這篇文章主要介紹了C++基于隨機數(shù)如何實現(xiàn)福彩雙色球,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

具體如下:

這是前段時間寫的福彩雙色球一個小應(yīng)用

本來可以一個文件搞定,反正也沒多大,就分開了.

頭文件doubleColorBallR2.h

#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

實現(xiàn)文件 doubleColorBallR2.cpp

#include "doubleColorBallR2.h"
void DoubleColorBallR2::getRedZone(){
  vector<int> v_red; //選擇數(shù)的范圍 1-_RED_ZONE_
  list<int> l_red;  //裝入1-_RED_NUM_個數(shù)
  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已裝入的數(shù)
    vector<int>::iterator iter=find(v_red.begin(), v_red.end(), v_red[n]);
    v_red.erase(iter);
    --j; //由于v_red已經(jīng)刪除了一位數(shù),所以隨機數(shù)取值范圍要減少一位
  }
  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; //選擇數(shù)的范圍 1-_BLUE_ZONE_
  list<int> l_blue;  //裝入1-_BLUE_NUM_個數(shù)
  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已裝入的數(shù)
    vector<int>::iterator iter=find(v_blue.begin(), v_blue.end(), v_blue[n]);
    v_blue.erase(iter);
    --j; //由于v_red已經(jīng)刪除了一位數(shù),所以隨機數(shù)取值范圍要減少一位
  }
  l_blue.sort();
  for (list<int>::iterator i=l_blue.begin(); i!=l_blue.end(); ++i) {
    cout<<*i<<" ";
  }
}

主程序 doubleColorBall.cpp

#include <iostream>
#include <list>
#include "windows.h"
#include <stdio.h>
#include "doubleColorBallR2.h"
#define RED_ZONE 33 //紅區(qū)
#define BLUE_ZONE 16 //藍區(qū)
#define RED_NUM 6  //紅區(qū)位數(shù)
#define BLUE_NUM 1 //藍區(qū)位數(shù)
using namespace std;
int main (int argc, char *argv[])
{
  DoubleColorBallR2 dcb;
  dcb.getRedZone();
  dcb.getBlueZone();
  getchar();
  return(0);
}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“C++基于隨機數(shù)如何實現(xiàn)福彩雙色球”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI