(recursive) 666: Put the Apples

Source: Internet
Author: User

Describe

Put m the same apples on n the same plate, allow some plates to be empty, ask how many different ways?

(denoted by k) 5,1,1 and 1,5,1 are the same kind of sub-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 a space. 1<=m,n<=10.

Output

For each set of data entered M and N, the corresponding k is output in one line.

Sample input

1

3 ·

Sample output

8

I think

This problem is done with recursive thinking, we need to be clear, it should have two parameters, one is the number of apples m, one is the number of plates N.

Then we divide the case according to the comparison between M and N, noting that its repetition condition.

1. When M>n (Apple more), each plate must have at least one apple, so we need to consider how to put m-n an apple into the N plate.

2. When m<n (plates are more), this is actually the equivalent of putting m apples into m-plates.

3. When the M=n (apples and plates are as long), we can divide each one into an apple, that is, 1 times, and the empty plate on the case.

My Code

#include <iostream>using namespacestd;intFuncintMintN) {    if(n==0|| m==0){        return 0; }    if(n==m) {        return 1+func (m,n-1); }    if(m<N) {        returnfunc (m,m); }    if(m>N) {        returnFunc (m-n,n) +func (m,n-1); }}intMain () {intk=0; CIN>>K;  while(k) {intn,m; CIN>>m>>N; cout<<func (m,n) <<Endl; K--; }    return 0;}

My summary

Such topics should first determine the number of good parameters, and then find ways to identify the relationship between the parameters of the composition, thus to the answer.



(recursive) 666: Put the Apples

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.