I saw this problem on the website (I will not use the exact wording or mention the website)
Suppose a child receives pocket money on the 15th of each month, depending on what day he is on that date, say, he receives 1 coin Monday, 2 coins on Tuesday ... 7 coins on Sunday. What is the expected number of coins he will receive on the 15th random month?
At first I, although the probability of each of them would be 1/7, so the answer should be 4, but he said the wrong answer.
Then I thought a little more about how to choose a random month, and remembered that the calendar repeats every 400 years, so maybe it has something to do with it, so I wrote the following code:
int Date(int mn,int yr)
{
if( ( yr%400==0 || (yr%100!=0 && yr%4==0) ) && mn==2)
return 29;
if(mn==2)
return 28;
if(mn==4 || mn==6 || mn==9 || mn==11)
return 30;
return 31;
}
int main()
{
double coins=0;
int wk=0;
for(int yr=1;yr<=400;yr++)
{
for(int mn=1;mn<=12;mn++)
{
for(int dt=1;dt<=Date(mn,yr);dt++)
{
if(dt==15)
coins += wk%7 +1;
wk++;
}
}
}
cout<<setprecision(10)<<coins/12/400;
}
Output -
4.001666667
! !
, , , , , ?
-
int main()
{
double total=0;
for(int i=0;i<7;i++)
{
int wk=i;
double coins=0;
for(int yr=1;yr<=400;yr++)
{
for(int mn=1;mn<=12;mn++)
{
for(int dt=1;dt<=Date(mn,yr);dt++)
{
if(dt==15)
coins += wk%7 +1;
wk++;
}
}
}
cout<<setprecision(10)<<coins/12/400<<endl;
total += coins;
}
cout<<endl<<setprecision(10)<<total/7/12/400;
}
-
4.001666667
3.998333333
4.000833333
3.998958333
4
4.001041667
3.999166667
4
Soooo... ... 4.00666, 1 0001 , - , - ?
?
"" , , ?