程序最美(寻路)

你还在坚持练习你的技术吗?运动员天天训练,音乐家也会演练更难的曲章。你呢?

双色球的简单统计和预测

双色球的简单统计和预测

         从网上下载了今年截止目前(2013.11.19)的双色球数据,统计历来红色球和蓝色球出现的最大次数和最小次数。根据简单的统计结果预测下一期中奖号码。但是结果是不尽如人意的。预测120次统计结果,其中根据最大次数预测中奖次数为11次,根据最小次数预测中奖次数为2次,并且中奖结果都是五等奖(10元奖金)和六等奖(5元奖金),而120次购买彩票至少投入是240元,最高奖金也就是110元。显然,这是一笔亏本的生意。

         网上的数据如下:

复制代码
// History.txt
2013-11-19    2013136     04 06 14 16 18 26 06    369,164,734
2013-11-17    2013135     09 23 24 25 29 31 12    406,720,240
2013-11-14    2013134     01 17 18 19 25 29 10    372,289,714
2013-11-12    2013133     04 07 12 19 22 25 01    361,709,328
2013-11-10    2013132     20 21 22 23 25 27 12    420,848,208
2013-11-07    2013131     04 06 12 17 19 26 09    388,319,260
2013-11-05    2013130     01 03 15 16 31 33 08    385,080,972
2013-11-03    2013129     05 06 10 14 27 31 14    418,710,844
2013-10-31    2013128     07 13 17 19 25 31 08    381,844,688
2013-10-29    2013127     02 03 13 20 22 33 14    376,726,836
2013-10-27    2013126     04 10 19 27 31 33 16    408,866,622
2013-10-24    2013125     04 06 08 18 25 28 16    372,984,566
2013-10-22    2013124     03 09 15 23 25 30 07    347,249,642
2013-10-20    2013123     01 02 06 11 17 25 02    385,822,278
2013-10-17    2013122     07 10 13 15 26 27 11    345,544,080
2013-10-15    2013121     04 05 06 07 25 27 07    337,100,116
2013-10-13    2013120     05 06 13 18 23 31 11    404,684,072
2013-10-10    2013119     05 15 20 22 26 32 09    357,071,040
2013-10-08    2013118     02 03 17 22 32 33 16    343,511,272
2013-10-06    2013117     09 12 13 24 27 33 16    368,579,726
2013-10-03    2013116     12 15 21 26 32 33 07    312,633,700
2013-10-01    2013115     03 12 16 17 18 27 08    319,974,332
2013-09-29    2013114     04 06 17 21 23 33 07    398,506,774
2013-09-26    2013113     04 07 11 17 24 33 09    353,992,568
2013-09-24    2013112     01 06 12 13 22 31 07    344,881,702
2013-09-22    2013111     01 02 03 06 08 33 13    401,502,238
2013-09-19    2013110     15 17 18 21 29 32 13    341,867,256
2013-09-17    2013109     09 23 24 27 29 32 08    351,155,742
2013-09-15    2013108     16 21 22 28 31 32 05    384,725,718
2013-09-12    2013107     07 09 11 17 28 31 11    349,907,348
2013-09-10    2013106     09 11 23 30 31 32 06    344,715,810
2013-09-08    2013105     01 11 23 27 31 32 09    380,197,404
2013-09-05    2013104     01 02 04 15 17 28 11    339,771,188
2013-09-03    2013103     02 04 09 13 18 20 07    329,389,008
2013-09-01    2013102     02 04 05 06 08 16 03    374,113,680
2013-08-29    2013101     05 07 09 23 27 32 01    339,246,710
2013-08-27    2013100     04 08 11 14 16 20 11    324,850,520
2013-08-25    2013099     05 11 20 21 26 31 03    359,490,506
2013-08-22    2013098     07 15 18 19 20 26 14    333,979,300
2013-08-20    2013097     05 14 17 22 23 25 07    326,153,582
2013-08-18    2013096     01 02 22 28 29 30 15    364,530,978
2013-08-15    2013095     01 06 15 19 28 29 10    321,839,422
2013-08-13    2013094     02 04 14 18 20 22 07    320,217,972
2013-08-11    2013093     01 02 08 26 29 31 14    345,826,922
2013-08-08    2013092     07 11 15 21 26 31 06    311,052,518
2013-08-06    2013091     04 14 24 25 28 31 10    308,670,790
2013-08-04    2013090     02 05 11 23 24 29 08    372,773,264
2013-08-01    2013089     04 08 12 19 21 25 13    337,088,774
2013-07-30    2013088     09 15 20 21 22 24 14    332,225,112
2013-07-28    2013087     02 13 19 23 24 28 05    364,306,204
2013-07-25    2013086     02 04 11 13 16 26 11    326,453,274
2013-07-23    2013085     02 08 12 14 16 32 16    330,145,456
2013-07-21    2013084     05 07 09 11 20 21 03    373,588,034
2013-07-18    2013083     05 06 12 14 19 23 09    338,036,388
2013-07-16    2013082     04 13 14 20 22 30 06    326,611,872
2013-07-14    2013081     01 04 10 13 21 31 13    359,745,012
2013-07-11    2013080     10 11 12 23 28 32 16    327,918,824
2013-07-09    2013079     07 13 17 19 22 26 13    320,512,624
2013-07-07    2013078     03 05 17 18 26 27 15    355,637,236
2013-07-04    2013077     09 14 23 24 26 29 03    333,114,166
2013-07-02    2013076     14 23 24 26 29 30 03    326,689,120
2013-06-30    2013075     04 06 09 25 30 33 14    358,276,246
2013-06-27    2013074     03 06 08 14 19 32 03    320,654,782
2013-06-25    2013073     02 09 13 17 20 28 11    324,559,238
2013-06-23    2013072     02 08 11 14 19 33 09    351,392,580
2013-06-20    2013071     05 12 21 23 26 28 09    311,914,552
2013-06-18    2013070     02 03 09 10 28 30 06    296,577,242
2013-06-16    2013069     07 16 17 18 30 33 06    369,339,396
2013-06-13    2013068     02 07 13 20 25 27 06    329,872,238
2013-06-11    2013067     01 04 09 15 22 30 06    309,743,864
2013-06-09    2013066     01 03 16 17 20 32 07    368,087,166
2013-06-06    2013065     07 18 19 23 29 30 02    334,460,176
2013-06-04    2013064     01 09 13 22 25 32 12    328,102,052
2013-06-02    2013063     10 15 18 20 23 31 12    354,390,256
2013-05-30    2013062     01 06 07 19 22 27 02    313,979,568
2013-05-28    2013061     05 06 07 12 13 18 12    335,064,726
2013-05-26    2013060     05 07 10 13 19 20 15    368,726,696
2013-05-23    2013059     03 10 18 24 27 29 09    339,381,758
2013-05-21    2013058     08 11 17 21 23 24 05    338,075,776
2013-05-19    2013057     13 16 19 23 26 28 05    367,207,180
2013-05-16    2013056     03 12 25 26 28 29 16    330,573,576
2013-05-14    2013055     08 18 19 22 27 32 06    328,924,474
2013-05-12    2013054     03 04 08 14 21 28 14    367,209,526
2013-05-09    2013053     03 12 13 22 30 33 14    327,360,018
2013-05-07    2013052     06 07 14 21 22 24 13    320,719,928
2013-05-05    2013051     08 12 15 19 28 29 02    377,856,082
2013-05-02    2013050     03 07 13 18 22 25 03    331,969,178
2013-04-30    2013049     10 13 14 16 21 32 14    295,889,902
2013-04-28    2013048     10 13 17 28 30 32 04    379,712,106
2013-04-25    2013047     01 08 11 17 27 30 12    347,274,348
2013-04-23    2013046     03 16 19 20 24 26 06    352,094,234
2013-04-21    2013045     06 07 08 14 23 31 12    389,847,482
2013-04-18    2013044     03 05 11 18 26 28 06    355,471,956
2013-04-16    2013043     03 06 14 15 17 25 16    358,568,922
2013-04-14    2013042     01 08 12 13 15 33 03    396,763,590
2013-04-11    2013041     02 10 12 17 23 24 05    358,260,706
2013-04-09    2013040     02 04 10 12 17 30 10    352,650,578
2013-04-07    2013039     01 02 14 15 24 29 06    390,740,078
2013-04-04    2013038     09 10 12 14 15 19 11    328,400,842
2013-04-02    2013037     02 15 18 27 28 32 14    350,336,028
2013-03-31    2013036     04 05 09 27 29 31 13    393,497,514
2013-03-28    2013035     01 14 15 17 26 30 02    351,498,872
2013-03-26    2013034     06 15 20 22 26 33 09    339,315,898
2013-03-24    2013033     05 06 13 17 19 28 01    397,248,028
2013-03-21    2013032     04 21 25 29 30 33 03    365,616,360
2013-03-19    2013031     03 13 14 15 21 33 03    365,951,410
2013-03-17    2013030     07 14 18 25 26 29 06    405,193,314
2013-03-14    2013029     06 07 10 19 23 29 12    367,189,222
2013-03-12    2013028     07 08 14 25 26 28 13    363,477,764
2013-03-10    2013027     01 02 04 12 21 24 12    401,897,672
2013-03-07    2013026     04 11 14 15 22 31 11    357,237,076
2013-03-05    2013025     16 17 18 24 25 30 08    365,053,994
2013-03-03    2013024     04 05 13 23 27 30 09    397,860,528
2013-02-28    2013023     03 06 15 18 30 32 05    359,844,386
2013-02-26    2013022     02 04 07 09 15 20 07    349,104,140
2013-02-24    2013021     01 06 17 19 26 31 11    386,291,388
2013-02-21    2013020     01 07 08 12 16 21 01    337,611,102
2013-02-19    2013019     01 02 05 16 20 26 06    316,958,200
2013-02-17    2013018     02 08 13 28 29 30 05    364,752,666
2013-02-07    2013017     04 06 12 30 31 32 09    339,247,106
2013-02-05    2013016     02 05 06 12 14 28 05    327,207,104
2013-02-03    2013015     05 06 07 11 13 18 15    377,989,410
2013-01-31    2013014     02 04 05 17 19 20 08    348,187,310
2013-01-29    2013013     05 06 13 19 22 28 09    351,745,408
2013-01-27    2013012     06 14 17 22 28 29 02    389,114,476
2013-01-24    2013011     03 12 17 24 27 29 09    350,274,074
2013-01-22    2013010     01 09 11 17 32 33 12    347,709,230
2013-01-20    2013009     01 04 09 13 16 23 02    383,674,016
2013-01-17    2013008     03 08 17 21 25 32 15    341,795,384
2013-01-15    2013007     02 09 15 22 26 32 01    341,246,158
2013-01-13    2013006     09 10 13 17 22 30 13    374,366,198
2013-01-10    2013005     01 13 14 25 31 32 12    339,352,226
2013-01-08    2013004     06 10 16 20 27 32 08    337,097,616
2013-01-06    2013003     22 23 26 27 28 33 09    367,773,688
2013-01-03    2013002     01 16 18 22 28 30 12    303,822,912
2013-01-01    2013001     06 08 14 15 24 25 06    309,153,922
复制代码

         具体的统计和预测程序如下:

复制代码
// 双色球简单统计和预测
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;

struct HistoryRecord
{
    string      date;
    string      issue;
    vector<int> red;
    int         blue;

    friend bool operator <  (const HistoryRecord& lhs, const HistoryRecord& rhs);
    friend bool operator == (const HistoryRecord& lhs, const HistoryRecord& rhs);
};

bool operator < (const HistoryRecord& lhs, const HistoryRecord& rhs)
{
    return lhs.date < rhs.date;
}

bool operator == (const HistoryRecord& lhs, const HistoryRecord& rhs)
{
    return lhs.date == rhs.date;
}

bool ReadHistoryRecord(const string& file, vector<HistoryRecord>& hisRecs)
{
    ifstream fin(file.c_str());
    if (!fin)
    {
        cerr << "File error!" << endl;
        return false;
    }

    hisRecs.clear();

    string        line;
    int           tmpInt;

    while (getline(fin, line))
    {
        istringstream sin(line);
        HistoryRecord tmpHr;
        sin >> tmpHr.date >> tmpHr.issue;
        for (int i = 0; i != 6; ++i)
        {
            sin >> tmpInt;
            tmpHr.red.push_back(tmpInt);
        }
        sin >> tmpHr.blue;

        hisRecs.push_back(tmpHr);
    }

    return true;
}

void
Statistics(
    const vector<HistoryRecord>& HisRecs,
    int rangeLeft,
    int rangeRight,
    map<int, double>& reds,
    map<int, double>& blues
    )
{
    reds.clear();
    blues.clear();

    for (int i = 1; i != 34; ++i)
    {
        reds[i] = 0.0;
    }
    for (int i = 1; i != 17; ++i)
    {
        blues[i] = 0.0;
    }

    for (int i = rangeLeft; i != rangeRight; ++i)
    {
        for (int j = 0; j != HisRecs[i].red.size(); ++j)
        {
            //cout << HisRecs[i].red[j] << '\t';
            ++reds[HisRecs[i].red[j]];
        }
        //cout << HisRecs[i].blue << endl;
        ++blues[HisRecs[i].blue];
    }
}

void
ForecastMax(
    const map<int, double>& reds,
    const map<int, double>& blues,
    vector<int>&            resultMax
    )
{
    resultMax.clear();

    multimap<double, int> resMax;
    int tmpBlue = -1;
    double timesBlue = -10.0;

    for (auto cit = reds.begin(); cit != reds.end(); ++cit)
    {
        if (resMax.size() < 6)
        {
            resMax.insert(make_pair(cit->second, cit->first));
        }
        else
        {
            if (cit->second > resMax.begin()->first)
            {
                resMax.erase(resMax.begin());
                resMax.insert(make_pair(cit->second, cit->first));
            }
        }
    }
    for (auto cit = blues.begin(); cit != blues.end(); ++cit)
    {
        if (cit->second > timesBlue)
        {
            tmpBlue = cit->first;
            timesBlue = cit->second;
        }
    }

    for (auto cit = resMax.begin(); cit != resMax.end(); ++cit)
    {
        resultMax.push_back(cit->second);
    }
    sort(resultMax.begin(), resultMax.end());

    resultMax.push_back(tmpBlue);
}

void
ForecastMin(
    const map<int, double>& reds,
    const map<int, double>& blues,
    vector<int>&            resultMin
    )
{
    resultMin.clear();

    multimap<double, int> resMin;
    int tmpBlue = -1;
    double timesBlue = 99999999999999.9;

    for (auto cit = reds.begin(); cit != reds.end(); ++cit)
    {
        if (resMin.size() < 6)
        {
            resMin.insert(make_pair(cit->second, cit->first));
        }
        else
        {
            auto tmpCit = resMin.end();
            --tmpCit;
            if (cit->second < tmpCit->first)
            {
                resMin.erase(tmpCit);
                resMin.insert(make_pair(cit->second, cit->first));
            }
        }
    }

    for (auto cit = blues.begin(); cit != blues.end(); ++cit)
    {
        if (cit->second < timesBlue)
        {
            tmpBlue = cit->first;
            timesBlue = cit->second;
        }
    }

    for (auto cit = resMin.begin(); cit != resMin.end(); ++cit)
    {
        resultMin.push_back(cit->second);
    }
    sort(resultMin.begin(), resultMin.end());

    resultMin.push_back(tmpBlue);
}

template <typename T>
void Display(const T& result)
{
    for (auto i = 0; i != result.size(); ++i)
    {
        cout << result[i] << '\t';
    }
    cout << endl << endl;
}

void Print(const map<int, double>& rb)
{
    int count = 0;
    for (auto cit = rb.begin(); cit != rb.end(); ++cit)
    {
        cout << cit->first << ':' << cit->second << '\t';
        count += cit->second;
    }
    cout << endl;
    cout << count << endl << endl;
}

bool Judge(const vector<int>& real, const vector<int>& result, int& grade)
{
    assert(real.size() == result.size() && result.size() == 7);
    grade = 9;

    int count = 0;
    int i = 0, j = 0;
    while (i < 6 && j < 6)
    {
        if (real[i] < result[j])
        {
            ++i;
        }
        else if (real[i] == result[j])
        {
            ++count;
            ++i;
            ++j;
        }
        else
        {
            ++j;
        }
    }
    if (count == 6 && real[6] == result[6])
    {
        grade = 1;
        return true;
    }
    else if (count == 6 && real[6] != result[6])
    {
        grade = 2;
        return true;
    }
    else if (count == 5 && real[6] == result[6])
    {
        grade = 3;
        return true;
    }
    else if (count == 5 && real[6] != result[6] || count == 4 && real[6] == result[6])
    {
        grade = 4;
        return true;
    }
    else if (count == 4 && real[6] != result[6] || count == 3 && real[6] == result[6])
    {
        grade = 5;
        return true;
    }
    else if (count <= 2 && real[6] == result[6])
    {
        grade = 6;
        return true;
    }
    return false;
}

int main()
{
    vector<HistoryRecord> hisRecs;
    if (!ReadHistoryRecord("History.txt", hisRecs))
    {
        exit(1);
    }

    //map<int, double> reds, blues;
    //Statistics(hisRecs, 0, hisRecs.size(), reds, blues);

    //Print(reds);
    //Print(blues);

    //vector<int> resultMax, resultMin;
    //ForecastMax(reds, blues, resultMax);
    //Display(resultMax);

    //ForecastMin(reds, blues, resultMin);
    //Display(resultMin);

    int countMax = 0, countMin = 0;
    for (int i = 120; i >= 1; --i)
    {
        map<int, double> reds, blues;
        Statistics(hisRecs, i, hisRecs.size(), reds, blues);
        
        vector<int> resultMax, resultMin;
        ForecastMax(reds, blues, resultMax);
        ForecastMin(reds, blues, resultMin);

        vector<int> real = hisRecs[i - 1].red;
        real.push_back(hisRecs[i - 1].blue);
        int grade = 9;
        if (Judge(real, resultMax, grade))
        {
            cout << "Max:\n";
            Display(real);
            Display(resultMax);
            cout << i << ":\t" << grade << endl << endl;
            ++countMax;
        }

        if (Judge(real, resultMin, grade))
        {
            cout << "Min:\n";
            Display(real);
            Display(resultMin);
            cout << i << ":\t" << grade << endl << endl;
            ++countMin;
        }
    }
    cout << countMax << endl << countMin << endl << endl;

    return 0;
}
复制代码

         之前也曾尝试预测NBA体育彩票,但是结果也不尽如人意。

         有关彩票方面的预测纯属娱乐。

posted on   unixfy  阅读(989)  评论(1编辑  收藏  举报

努力加载评论中...

导航

点击右上角即可分享
微信分享提示