抽屉原理——放苹果

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
int pg,pz,js=0;

int cnm(int m,int n)
{
if(n==1)return 1;
if(m==1)return 1;
if(m<n)return cnm(m,m);//每个抽屉1个,剩下的空抽屉舍了
if(m==n)return 1+cnm(m,m-1);//让空抽屉再多一个
if(m>n)return cnm(m,n-1)+cnm(m-n,n);//每次多一个空抽屉,而让每个抽屉至少有一个后,多出来的部分,再填入不为空的抽屉中
}

int main()
{
int t;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>pg>>pz;
cout<<cnm(pg,pz)<<endl;
} 
return 0;
}

代码非原创,链接http://noi.openjudge.cn/topic/52854/

我说一下思路。这道题就是把题意转化为抽屉原理。

先确定退出条件,m=n=1

再根据不为空的抽屉数,来递归出状态数。

 

posted @ 2019-03-21 22:54  风间6324  阅读(297)  评论(0编辑  收藏  举报