把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法。
Input
第一行是测试数据的数目t(0 <= t <= 20)。以下每行均包含二个整数M和N,以空格分开。1<=M,N<=10。
Output
对输入的每组数据M和N,用一行输出相应的K。
Sample Input
1
7 3
Sample Output
8
递归写就行了
7个苹果放三个盘子
分两种
7 0 0 5 1 1 7 0 4 0 0
1 6 0 4 2 1==1 6 3 1 0
2 5 0 3 2 2==2 5 2 1 1
3 4 0 3 3 1 3 4 2 2 0
第一种是有0的那么第一组就和7放 2 一样了,7放2又分为有0组和无0组
继续直到变成7放1,就只有一种了返回1
第二种是无0的那么第二组全减去1之后个数也是不会变的,就变成了7-3放3了
于是4放3又可以分成有0组和无0组,直到变成1放3,和1放1一样返回1
JAVA代码体现:
import java.util.Scanner;
public class Main {
public static void main(String []ages)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
while(n--!=0)
{
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println(count(a,b));
}
}
static int count(int apple,int plant)
{
if(apple==0||plant==1)
return 1;
if(plant>apple)
{
return count(apple,apple);
}
else
{
return (count(apple,plant-1)+count(apple-plant,plant));
}
}
}