-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbirtday.cpp
60 lines (50 loc) · 1.27 KB
/
birtday.cpp
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
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <set>
using namespace std;
string generateBirthday(){
int month = rand() % 12 + 1;
int day;
if(month == 2) {
day = rand() % 28 + 1;
}
else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
day = rand() % 31 + 1;
}
else {
day = rand() % 30 + 1;
}
string birthday = to_string(day) + '/' + to_string(month);
return birthday;
}
bool simulation(int babyCount) {
set<string> simulationBirthdaySet;
for (int i = 0; i < babyCount ; i++) {
auto check = simulationBirthdaySet.insert(generateBirthday());
if(!check.second)
return true; //true = matching birthday
}
return false;
}
double possibilityCheck(int babyCount, int simulationCount){
int trueCount = 0;
for(int i = 0; i < simulationCount; i++) {
if(simulation(babyCount))
trueCount++;
}
cout << trueCount << endl;
return (double)trueCount / (double)simulationCount;
}
int main(){
srand(time(0));
int simulationCount = 0;
int babyCount = 0;
cout << "Simulation count: ";
cin >> simulationCount;
cout << "Baby Count: ";
cin >> babyCount;
cout << possibilityCheck(babyCount, simulationCount) << endl;
return EXIT_SUCCESS;
}