Skip to content

Commit 896be9e

Browse files
committed
srm 174 div 1 250
1 parent dac1d54 commit 896be9e

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

BirthdayOdds.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <cstring>
8+
#include <climits>
9+
using namespace std;
10+
typedef pair<int,int> pi;
11+
typedef vector<int> vi;
12+
typedef vector<vi> vvi;
13+
typedef vector<string> vs;
14+
typedef vector<vs> vvs;
15+
16+
class BirthdayOdds {
17+
public:
18+
int minPeople(int minOdds, int daysInYear)
19+
{
20+
int n = daysInYear;
21+
double pr = 100 - minOdds;
22+
double p = 100.00;
23+
int k = 1;
24+
while(p > pr){
25+
p *= (double)(n-k)/double(n);
26+
k++;
27+
}
28+
return k;
29+
}
30+
31+
// BEGIN CUT HERE
32+
public:
33+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); }
34+
private:
35+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
36+
void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
37+
void test_case_0() { int Arg0 = 75; int Arg1 = 5; int Arg2 = 4; verify_case(0, Arg2, minPeople(Arg0, Arg1)); }
38+
void test_case_1() { int Arg0 = 50; int Arg1 = 365; int Arg2 = 23; verify_case(1, Arg2, minPeople(Arg0, Arg1)); }
39+
void test_case_2() { int Arg0 = 1; int Arg1 = 365; int Arg2 = 4; verify_case(2, Arg2, minPeople(Arg0, Arg1)); }
40+
void test_case_3() { int Arg0 = 84; int Arg1 = 9227; int Arg2 = 184; verify_case(3, Arg2, minPeople(Arg0, Arg1)); }
41+
42+
// END CUT HERE
43+
44+
};
45+
46+
// BEGIN CUT HERE
47+
int main()
48+
{
49+
BirthdayOdds ___test;
50+
___test.run_test(-1);
51+
}
52+
// END CUT HERE

BirthdayOdds.txt

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
PROBLEM STATEMENT
2+
Here is an interesting factoid: "On the planet Earth, if there are at least 23 people in a room, the chance that two of them have the same birthday is greater than 50%." You would like to come up with more factoids of this form. Given two integers (minOdds and daysInYear), your method should return the fewest number of people (from a planet where there are daysInYear days in each year) needed such that you can be at least minOdds% sure that two of the people have the same birthday. See example 0 for further information.
3+
4+
DEFINITION
5+
Class:BirthdayOdds
6+
Method:minPeople
7+
Parameters:int, int
8+
Returns:int
9+
Method signature:int minPeople(int minOdds, int daysInYear)
10+
11+
12+
NOTES
13+
-Two people can have the same birthday without being born in the same year.
14+
-You may assume that the odds of being born on a particular day are (1 / daysInYear).
15+
-You may assume that there are no leap years.
16+
17+
18+
CONSTRAINTS
19+
-minOdds will be between 1 and 99, inclusive.
20+
-daysInYear will be between 1 and 10000, inclusive.
21+
-For any number of people N, the odds that two people will have the same birthday (in a room with N people, on a planet with daysInYear days in each year) will not be within 1e-9 of minOdds. (In other words, you don't need to worry about floating-point precision for this problem.)
22+
23+
24+
EXAMPLES
25+
26+
0)
27+
75
28+
5
29+
30+
Returns: 4
31+
32+
We must be 75% sure that at least two of the people in the room have the same birthday. This is equivalent to saying that the odds of everyone having different birthdays is 25% or less.
33+
34+
If there is only one person in the room, the odds are 5/5 or 100% that nobody shares a birthday.
35+
If there are two people in the room, the odds are 5/5 * 4/5 = 80% that nobody shares a birthday. This is because the second person has 4 "safe" days on which his birthday could fall, out of 5 possible days in the year.
36+
If there are three people in the room, the odds of no overlap are 5/5 * 4/5 * 3/5 = 48%.
37+
If there are four people in the room, the odds are 5/5 * 4/5 * 3/5 * 2/5 = 19.2%. This means that you can be (100% - 19.2%) = 80.8% sure that two or more of them do, in fact, have the same birthday.
38+
39+
We only need to be 75% sure of this, which was untrue for three people but true for four. Therefore, your method should return 4.
40+
41+
1)
42+
50
43+
365
44+
45+
Returns: 23
46+
47+
The factoid from the problem statement. If there are 22 people in a room, the odds of a shared birthday are roughly 47.57%. With 23 people, these odds jump to 50.73%, which is greater than or equal to the 50% needed.
48+
49+
2)
50+
1
51+
365
52+
53+
Returns: 4
54+
55+
Another example from planet Earth. The odds of a repeat birthday among only four people are roughly 1.64%.
56+
57+
3)
58+
84
59+
9227
60+
61+
Returns: 184

0 commit comments

Comments
 (0)