Blue Bridge cup-looking for money, Blue Bridge cup for money
Problem description:
The park price is 5. Assume that each visitor only holds two currencies: 5 cents and 1 yuan. Let us assume that there are m people holding 5 corners and n people holding 1 yuan. Due to special circumstances, the conductor had no change at the beginning. We want to know the order in which m + n visitors can purchase tickets. Obviously, m<N can't be completed in any way; m> = n, some situations won't work. For example, the first passenger who buys a ticket holds 1 yuan. Calculate the number of combinations of all m + n visitors that may successfully complete the ticket purchase.
Note: We only care about the order in which the value of 5 and 1 yuan alternate. The exchange positions of two tourists holding the same currency value are not counted in a new situation.
Problem Analysis:
With recursive thinking, we can start from the last person buying tickets. Suppose (m + N-1) people have already bought tickets, for the m + n person, he has two kinds of buying tickets, buy tickets with a 5-angle ticket, or buy tickets with a 1 Yuan ticket, so get the relationshipSolve (m-1, n)OrSolve (m, n-1), F is the ticket buying function, and for the (m + N-1) individual, because it has been assumed that at this time (m + N-2) individual has bought a good ticket, then there are two kinds of buying a ticket, buy a ticket with a 5-angle ticket or 1 Yuan ticket ,...., finally, when the second person buys a ticket, he has two kinds of buying a ticket, holding a 5-angle ticket, or holding a 1 Yuan ticket, and finally until the first person, as described above.
Next, we will study function export:
In the course of buying a ticket, if m <n, the ticket must fail. If this is the first recursive exit, return directly.
Observe the second person's ticket purchase situation, we can find that if he holds a 5-angle (m = 1), then the first person must hold 1 yuan, because m> = n, at this time, n must be 1. At this time, there is only one situation ,... 51. If this is the second recursive exit, count ++ will execute return.
When one of the people who buy a ticket has no more than one yuan, the rest of the people have five horns, there is only one situation ,... 5555, in order to establish the relationship, this is the third recursive exit, count ++, and then execute return.
Code Description:
1 # include <stdio. h>
2 int count = 0;
3 void solve (int m, int n) {
4/*
5 m indicates the number of people holding corner 5, n indicates the number of people holding 1 yuan
6. The last person is the site
7 */
8 if (m <n) return; // when the number of people is reduced, m <n then cannot find money, recursive exit
9 if (m = 1) {// when the penultimate is 5, there is only one situation .... 51
10 count ++;
11 return;
12}
13 if (n = 0) // when the number of 1 yuan is 0, there is only one situation... 55555...
14 {
15 count ++;
16 return;
17}
18/* each return serves as the end of the function */
19 solve (m-1, n ); // buy
20 solve (m, n-1) for a person in corner 5; // buy
21
22}
23 int main () {
24 solve );
25 printf ("% d", count);
26 return 0;
27}
The running result is as follows: