-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasy_Prob124.cpp
More file actions
49 lines (36 loc) · 971 Bytes
/
Easy_Prob124.cpp
File metadata and controls
49 lines (36 loc) · 971 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* 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.*/
#include <iostream>
#include <cmath>
using namespace std;
int flipCoins(int n)
{
float random = 0.0;
int stayingCoin = n;
for (int i=0; i<n; i++)
{
random = (float)(rand()) / (float)(RAND_MAX);
if (random >= 0.5)
{
stayingCoin -= 1;
}
}
return stayingCoin;
}
int calculRounds(int n)
{
int stayingCoin = n;
int round = 0;
while (stayingCoin > 0)
{
round += 1;
stayingCoin = flipCoins(stayingCoin);
}
return round;
}
int main(int argc, char *argv[])
{
int n = 5;
srand(time(0));
cout << calculRounds(n) << endl;
}