0

似乎无法让它发挥作用。这个想法是在每次计数后计算正面和反面的百分比,在每次迭代后累积。除了我的计算不断得到nan%。有人看到我做错了什么吗?

void flipCoin(time_t seconds, int flipCount){
    vector<int> flips;
    float headCount = 0;
    float tailCount = 0;
    double headProbability = double((headCount/(headCount + tailCount))*100);
    double tailProbability = double((tailCount/(headCount + tailCount))*100);

    for (int i=0; i < flipCount; i++) {
        int flip = rand() % (HEADS - TAILS + 1) + TAILS;
        flips.push_back(flip);
        if (flips[i] == 1) {
            tailCount++;
            cout << "Tail Percent: " << tailProbability << "%" << endl;
        }else{
            headCount++;
            cout << "Head Percent: " << headProbability << "%" << endl;
        }
    }
}
4

3 回答 3

2

你想这样做

void flipCoin(time_t seconds, int flipCount){
    vector<int> flips;
    float headCount = 0;
    float tailCount = 0;
    double headProbability = 0;
    double tailProbability = 0;

    for (int i=0; i < flipCount; i++) {
        int flip = rand() % (HEADS - TAILS + 1) + TAILS;
        flips.push_back(flip);
        if (flips[i] == 1) {
            tailCount++;
            tailProbability = double((tailCount/(headCount + tailCount))*100);
            cout << "Tail Percent: " << tailProbability << "%" << endl;
        }else{
            headCount++;
            headProbability = double((headCount/(headCount + tailCount))*100);
            cout << "Head Percent: " << headProbability << "%" << endl;
        }
    }
}

此外,如果您只想打印百分比,则不需要 tailProbability 或 headProbability。您可以直接打印计算结果。

编辑:您也可以用 i+1 替换 headCount + tailCount。但是,如果您稍后更改该功能,这可能会导致问题。

于 2012-11-28T05:08:18.993 回答
1

您的问题在于您只计算headProbabilitytailProbability一次 - 当您计算它们时,它将评估为0 / 0

float headCount = 0;
float tailCount = 0;
// Both are 0, end up as 0/0
double headProbability = double((headCount/(headCount + tailCount))*100);  
double tailProbability = double((tailCount/(headCount + tailCount))*100);

//Never recalculated in the loop

要解决此问题,只需最初将它们定义为 0,然后在循环中重新计算它们。您似乎也不需要在vector这里,因为您只需要跟踪计数,而不是每次翻转的结果:

double headProbability = 0;
double tailProbability = 0;

for(int i = 0; i < flipCount; ++i) {
    int flip = rand() % (HEAS - TAIL + 1) + TAILS; //This could be simplified
    if(flip == 1) {
        ++tailCount;
        //Recalculate tailProbability
    } else { 
       //Etc...
    }
}
于 2012-11-28T05:02:15.410 回答
0

nan表示不是数字。你得到它是因为你除以 0。

在任何抛硬币之前,您的代码只计算一次概率。您需要将计算放入循环中,以便在每次迭代后重新计算概率。

void flipCoin(time_t seconds, int flipCount){
    vector<int> flips;
    float headCount = 0;
    float tailCount = 0;
    double headProbability;
    double tailProbability;

    for (int i=0; i < flipCount; i++) {
        int flip = rand() % (HEADS - TAILS + 1) + TAILS;
        flips.push_back(flip);

        headProbability = double((headCount/(headCount + tailCount))*100);
        tailProbability = double((tailCount/(headCount + tailCount))*100);

        if (flips[i] == 1) {
            tailCount++;
            cout << "Tail Percent: " << tailProbability << "%" << endl;
        }else{
            headCount++;
            cout << "Head Percent: " << headProbability << "%" << endl;
        }
    }
}
于 2012-11-28T05:05:21.633 回答