Description
把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 3Sample Output
8Source
lwx@POJ 我怎么也遇上放苹果,感觉自己萌萌哒。 简单递归。分为每一个盘都能放一个和至少有一个盘子为空。 import java.util.*; public class Main { public static int f(int m,int n){ if(m<0) return 0; if(m==0||n==1) return 1;//m=0每一个放一个正好放完,n=1最后仅仅剩一个盘子 return f(m-n,n)+f(m,n-1); } public static void main(String[] args) { Scanner scan=new Scanner(System.in); int t=scan.nextInt(); for(int i=0;i<t;i++){ int m=scan.nextInt(); int n=scan.nextInt(); System.out.println(f(m,n)); } } }版权声明:本文博主原创文章。博客,未经同意不得转载。
转载于:https://www.cnblogs.com/bhlsheji/p/4803223.html
相关资源:数据结构—成绩单生成器