首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++ DeathPrediction -第一个程序如何在for循环中添加precentage

C++ DeathPrediction -第一个程序如何在for循环中添加precentage
EN

Stack Overflow用户
提问于 2014-09-13 03:26:54
回答 2查看 84关注 0票数 0

我决定了我的第一个C++项目,我会做一个死亡预测器,它将有准确的死亡机会,并且会给你一个生日,在这个生日中,你会一直每隔360毫秒得到一次生日,直到你最终死去。

我面临的问题是,我不知道如何将前倾图表纳入我的工作。我只添加了19条if语句中的1条,因为线程上的600行代码对nagivate来说是一种痛苦。我如何结合这些前倾图表,使程序工作?

代码语言:javascript
复制
//GOOOD LUCK
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <random>

using namespace std;

int main()
{
    random_device rd;
    mt19937 gen(rd());

    uniform_int_distribution<> dis1 (1,28440);         // Died under 1 year of
    //age.  1.161756638 % chance

    uniform_int_distribution<> dis2 (28441,33196);     // Died between 1-4 years of
    //age.  0.194279696 % chance

    uniform_int_distribution<> dis3 (33197,36033);     // Died between 5-9 years of
    //age.  0.115889718 % chance

    uniform_int_distribution<> dis4 (36034,39798);     // Died between 10-14 years of
    //age.  0.153797951 % chance

    uniform_int_distribution<> dis5 (39799,53501);     // Died between 15-19 years of
    //age.  0.559759184 % chance

    uniform_int_distribution<> dis6 (53502,74032);     // Died between 20-24 years of
    //age.  0.838678816 % chance

    uniform_int_distribution<> dis7 (74033,93600);     // Died between 25-29 years of
    //age.  0.799340854 % chance

    uniform_int_distribution<> dis8 (93601,115957);    // Died between 30-34 years of
    //age.  0.913264801 % chance

    uniform_int_distribution<> dis9 (115958,147377);   // Died between 35-39 years of
    //age.  1.283487819 % chance

    uniform_int_distribution<> dis10(147378,200742);   // Died between 40-44 years of
    //age.  2.179927672 % chance

    uniform_int_distribution<> dis11(200743,280125);   // Died between 45-49 years of
    //age.  3.242747089 % chance

    uniform_int_distribution<> dis12(280126,384272);   // Died between 50-54 years of
    //age.  4.254341371 % chance

    uniform_int_distribution<> dis13(384273,511750);   // Died between 55-59 years of
    //age.  5.207398478 % chance

    uniform_int_distribution<> dis14(511751,659573);   // Died between 60-64 years of
    //age.  6.038474388 % chance

    uniform_int_distribution<> dis15(659574,831809);   // Died between 65-69 years of
    //age.  7.035735454 % chance

    uniform_int_distribution<> dis16(831810,1057928);  // Died between 70-74 years of
    //age.  9.236823110 % chance

    uniform_int_distribution<> dis17(1057929,1365816); // Died between 75-79 years of
    //age. 12.57703684 % chance

    uniform_int_distribution<> dis18(1365817,1744593); // Died between 80-84 years of
    //age. 15.47280922 % chance

    uniform_int_distribution<> dis19(1744594,2447762); // Died at age 85 and above.
    //     28.72402438 % chance


    //REFERENCE: http://www.disastercenter.com/cdc/Death%20rates%202005.html


    for (double count = 0; count < 101; count++)
    {
        int var1 = dis1(gen);
        int var2 = dis2(gen);
        int var3 = dis3(gen);
        int var4 = dis4(gen);
        int var5 = dis5(gen);
        int var6 = dis6(gen);
        int var7 = dis7(gen);
        int var8 = dis8(gen);
        int var9 = dis9(gen);
        int var10 = dis10(gen);
        int var11 = dis11(gen);
        int var12 = dis12(gen);
        int var13 = dis13(gen);
        int var14 = dis14(gen);
        int var15 = dis15(gen);
        int var16 = dis16(gen);
        int var17 = dis17(gen);
        int var18 = dis18(gen);
        int var19 = dis19(gen);

        if (count < 102)
        {
            Sleep(360);
            cout << "\n\t\t    It's your birthday! You turned: " << count;

        }
        if (var1 > 1 && var1 > 28440)
        {
            cout << "\n\n\n\t\t\tYou died! Better luck next time!";
            cout << "\n\n     ";
            Sleep(1000);
            cout << "T   "; Sleep(100);
            cout << "H   "; Sleep(100);
            cout << "A   "; Sleep(100);
            cout << "N   "; Sleep(100);
            cout << "K   "; Sleep(100);
            cout << "S   "; Sleep(100);
            cout << "    "; Sleep(100);
            cout << "F   "; Sleep(100);
            cout << "O   "; Sleep(100);
            cout << "R   "; Sleep(100);
            cout << "    "; Sleep(100);
            cout << "P   "; Sleep(100);
            cout << "L   "; Sleep(100);
            cout << "A   "; Sleep(100);
            cout << "Y   "; Sleep(100);
            cout << "I   "; Sleep(100);
            cout << "N   "; Sleep(100);
            cout << "G   "; Sleep(100);
            cout << "    "; Sleep(100);
            cout << "\n\n\n\t\t\t  ";
            return 0;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-14 13:08:38

你可以尝试这样的方法:

代码语言:javascript
复制
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<double> distribution(0, 100.);

unsigned int age = 0;
for (;;) {
    auto percent = distribution(gen);
    if (percent <= get_die_probability(age)) {
        break;
    }
    ++age;
    std::cout << "It's your birthday! You turned: " << age << std::endl;
}
std::cout << "You die at " << age << std::endl;

现场演示

票数 0
EN

Stack Overflow用户

发布于 2014-09-13 03:53:58

您需要一个统一分布对象的向量,而不是由数以百万计的单独命名的变量组成。

代码语言:javascript
复制
typedef uniform_int_distribution<> dt;
vector<dt> dis;

int low= 0;
for (...stuff...) {
  ++low;
  high= ...;
  dis.push_back(dt(low,high));
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25819532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档