gpt4 book ai didi

C++:计算每次迭代期间的概率百分比

转载 作者:行者123 更新时间:2023-11-28 00:52:04 25 4
gpt4 key购买 nike

似乎无法让它工作。这个想法是计算每次计数后正面和反面的百分比,在每次迭代后累积。除了我的计算不断得到 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;
}
}
}

最佳答案

你想这样做

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 或 headProbablity。您可以直接打印计算结果。

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

关于C++:计算每次迭代期间的概率百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13598107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com