题解 | #放苹果#

放苹果

https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf

import sys
#思路来源:https://blog.csdn.net/m0_45816418/article/details/109656592
def wayNum_setapple(apple_num,dish_num):
    #这个题目显然是要使用递归的方案的
    #输入判断
    if apple_num < 0 or dish_num < 1:
        print("输出有误,请注意苹果数和盘子数的范围。")
        return 0
    
    #递归出口
    if apple_num == 0 or dish_num == 1:
        return 1
    #递归过程
    #思路简介:盘子太多,则结果和没有这些空盘子一样
    #至少有一个空盘子,则wayNum_setapple(apple_num, dish_num-1)
    #没有空盘,至少有一个,那和苹果一盘少一个的分法是一样的:wayNum_setapple(apple_num-dish_num, dish_num)
    if apple_num < dish_num:
        return wayNum_setapple(apple_num, apple_num)
    else:
        return wayNum_setapple(apple_num, dish_num-1)+wayNum_setapple(apple_num-dish_num, dish_num)
        
apple_dish_num = input().split(' ')
apple_num ,dish_num = int(apple_dish_num[0]),int(apple_dish_num[1])
print(wayNum_setapple(apple_num,dish_num))

全部评论

相关推荐

06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
深夜书店vv:腾讯是这样的,去年很多走廊都加桌子当工区
点赞 评论 收藏
分享
07-07 11:33
江南大学 Java
已经在暑假实习了&nbsp;,没有明确说有hc,纠结实习到八月份会不会有点影响秋招毕竟感觉今年好多提前批
程序员小白条:92的话准备提前批,其他没必要,没面试机会的,而且你要准备充分,尤其八股和算法题
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务