2.3 基本算法之递归变递推 放苹果 python

http://noi.openjudge.cn/ch0203/666/

"""
2.3 基本算法之递归变递推 666 放苹果
http://noi.openjudge.cn/ch0203/666/

C和Python实现放苹果
https://zhuanlan.zhihu.com/p/340794311

Python如何实现佩尔数列?
https://www.zhihu.com/question/495732022/answer/2200069561

"""
def putInPlate(m, n):
            
	if(m== 1 or m==0 or n==1):
		return 1
	    
	if(m <0):
		return 0
	    
	return putInPlate(m-n,n)+putInPlate(m,n-1)

t=int(input())

a = 0

while a< t:
            m,n= map(int, input().split())

            if(n==0):
                        break;
            
            print(putInPlate(m,n))

            a += 1

参考:

C和Python实现放苹果
https://zhuanlan.zhihu.com/p/340794311

Python如何实现佩尔数列?
https://www.zhihu.com/question/495732022/answer/2200069561


C++代码:

/*
2.3基本算法之递归变递推_666放苹果
http://noi.openjudge.cn/ch0203/666/

1192:放苹果
http://ybt.ssoier.cn:8088/problem_show.php?pid=1192
*/
#include<iostream>
#include<cstring>
using namespace std;
int a[20][20];
int f(int m,int n)
{
    int i,j;
    for(i=1;i<=n;i++)//0个苹果
        a[0][i]=1;
    
	for(i=1;i<=m;i++)//1个盘子
        a[i][1]=1;
    
	for(i=1;i<=m;i++)
        for(j=2;j<=n;j++)
            if(i<j)
                a[i][j]=a[i][i];
            else 
                a[i][j]=a[i][j-1]+a[i-j][j]; 
}
int main()
{
    int m,n,i,j,k;
    
    cin>>k;
    
	for(i=1;i<=k;i++)
    {
        cin>>m>>n;
        f(m,n);
        cout<<a[m][n]<<endl;
    }
    
    return 0;
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dllglvzhenfeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值