SPOJ FUNPROB - Yanu in Movie theatre
题目链接:http://www.spoj.com/problems/FUNPROB/
题目大意:N+M个人排队买票,N个人手里有10元,M个人手里有5元,票价5元。但售票厅没有零钱了。问其能正常售票的概率(即不会出现10元买票但没零钱找)。
解题思路:参考:http://stackoverflow.com/questions/25281005/calculating-probability-for-funprob
当N < M 时的结果 res = (M - N + 1) / (M + 1) ,证明的话可以使用数学归纳法:
当N = 1 的时候 res = (M + 1 - 1) / (M + 1)。
假设当N = K的时候有res = (M + K - 1) / (M + 1),则当N = K + 1时,我们将第K+1个人放到已经能够正常工作的队列中时,每个10元的都能和前面一个5元的相互抵消,那么
res = (M - K + 1) / (M + 1) * (M - (K + 1) + 1) / (M - K + 1) = (M - (K + 1) + 1) / (M + 1)
即证。
推导过程详见参考链接。
代码:
1 #include <cstdio> 2 typedef long long ll; 3 ll n, m; 4 5 void solve(){ 6 if(n > m) printf("%.6f", 0); 7 else if(n == m) printf("%.6f", 1.0 / (m + 1)); 8 else printf("%.6f", (double)(m - n + 1) / (m + 1)); 9 puts(""); 10 } 11 int main(){ 12 while(scanf("%lld %lld", &n, &m) && (n || m)){ 13 solve(); 14 } 15 }
题目:
FUNPROB - Yanu in Movie theatre
Yanu is a great fan of Harry Potter.So on the day of the movie release, Yanu rushes to the movie theatre to watch the movie. On the release of the 6th movie, on reaching the theatre she found a long queue for tickets already.As the distirbution was about to start, the ticket counter did not have any change to start with. There are N+M people in queue,where N have Rs 10.00 and M have Rs 5.00. The ticket costs Rs 5.00.
Now Yanu is math geek too , now she wonders What is the probability that the theatre can always provide change.
Input
Each line contain N and M , seperated by a space , End of Input is marked by 0 0 which should not be processed. Both N and M fits in integer range.
Output
For each input , output the respective probability upto 6 decimal digits.
Example
Input:
1 0
0 1
41 41
0 0
Output:
0.000000
1.000000
0.023810