Eggs, Map of erguang Expressway

Source: Internet
Author: User

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);}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.