放苹果
来源:互联网 发布:mac重装系统保留文件 编辑:程序博客网 时间:2024/06/02 10:25
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18538 Accepted: 11749
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 3
Sample Output
8
答案
#include<stdio.h>
int M,N,K=0;
int b[100];
int a=0,k;
void apple(int t,int j)
{
if(t==0&&a<=N)
{
K++;
for(k=0;b[k]!=0;k++)
printf("%2d",b[k]);
printf("\n");
}
for(int i=M;i>0;i--)
if(t>=i&&i<=j)
{
t=t-i;
b[a++]=i;
apple(t,i);
b[a--]=0;
t=t+i;
}
}
void main()
{
int t;
scanf("%d",&t);
for(int i=0;i<t;i++)
{
//printf("输入M N\n");
scanf("%d %d",&M,&N);
apple(M,M);
printf("%d\n",K);
}
}