You have n
fair coins and you flip them all at the same time. Any that come up tails you set aside. The ones that come up heads you flip again. How many rounds do you expect to play before only one coin remains?
Write a function that, given n
, returns the number of rounds you'd expect to play until one coin remains.
Example:
Input: n=100
Output: Expected rounds to play: 7
Approach
C++
#include <bits/stdc++.h>using namespace std;int flip(){return (rand() % 100) > 49 ? 0 : 1;}int coinCounter(int totalCoins){int totalRounds = 0;int coinsToFlip = totalCoins;while (coinsToFlip > 1){int coins = coinsToFlip;for (int i = 0; i < coins; ++i){if (flip() == 1){--coinsToFlip;}}++totalRounds;}return totalRounds;}int main(){int numCoins = 100;int result = coinCounter(numCoins);cout << "Expected rounds to play: " << result << endl;return 0;}
No comments:
Post a Comment