Eggs, Map of erguang Expressway
Description:
Erin bought a lot of eggs and found that she could not eat so much in a day, so she decided to put n identical eggs in m of the same basket, and allowed some baskets to be empty, how many different release methods are there?
Note: 2, 1, 1, 2, and 1 are the same method.
Input
The first line is the number of test data t (0 <= t <= 20 ). Each of the following lines contains two integers, m and n, separated by spaces. 1 <= m, n <= 10.
Output
Output the corresponding results in one row for each group of input data m and n.
For example:
Input:
4
3 8
4 7
2 4
4 2
Output:
10
11
3
2
(Note that there is a line break at the end)
Hint:
Use recursion to analyze and solve problems, that is, how to return results under different circumstances.
You can try a tree chart (but I don't think it is useful ).
Note that the basket can be left empty. You need to understand the last two examples in the example before proceeding with the question.
My code:
#include<stdio.h>int egg(int m, int n);int main() { int t, m, n, i, result = 0; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d%d", &m, &n); result = egg(m, n); printf("%d\n", result); } return 0;}int egg(int m, int n) { if (m == 1 || n == 1) { return 1; } if (n <= m) { return 1 + egg(n-1, n); } else { return egg(m-1, n) + egg(m, n-m); }}
Answer:
#include<stdio.h>int egg(int m, int n); int main() { int t; // m for baskets, n for eggs int m, n; int result = 0; scanf("%d", &t); while (t--) { scanf("%d %d", &m, &n); result = egg(m , n); printf("%d\n", result); } return 0;} int egg(int m, int n) { if (m == 1 || n == 0) return 1; if (n < 0) return 0; return egg(m-1, n) + egg(m, n-m);}