九度 Online Judge 算法 刷题 题目1160:放苹果

时间:2022-06-25 18:54:50

题目1160:放苹果

题目描述:
把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。
输入:
第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。
输出:
对输入的每组数据M和N,用一行输出相应的K。
样例输入:
1
7 3
样例输出:
8
来源:
2011年北京大学计算机研究生机试真题

code

java 实现

import java.util.Scanner;

public class Main {
public static int apple(int m,int n){
if (m < n)
return apple(m,m);
else if ( n == 1 )
return 1;
else if( m == 0 )
return 1;
else
return apple(m,n-1)+apple(m-n,n);

}
public static void main(String[] args) {
Scanner shuru = new Scanner(System.in);
while(shuru.hasNext())
{
int a = shuru.nextInt();
for (int i = 0; i < a; i++)
{
int m = shuru.nextInt();
int n = shuru.nextInt();
System.out.println(apple(m, n));
}
}
}
}

/**************************************************************
Problem: 1160
User: langzimaizan
Language: Java
Result: Accepted
Time:80 ms
Memory:15464 kb
****************************************************************/