题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
/* * @Description: If you get question about the code, contact me at **********. * @Author: pengjunxing * @Date: 2023-02-08 14:47:20 * @LastEditors: pengjunxing * @LastEditTime: 2023-02-10 19:21:19 */ #include<iostream> using namespace std; int fun(int apple,int pan) { //递归法 //递归出口1为:苹果数或盘子数为1时,这时算一种情况 //递归出口2为:盘子数或苹果数小于零时,此时不符合实际返回0 //递归表达式:apple个苹果放在n-1个盘子+ if(apple<0||pan<0) { return 0; } else if(apple==1||pan==1) { return 1; } else { return fun(apple,pan-1)+fun(apple-pan,pan); } } int main() { int num1,num2; cin>>num1>>num2; cout<<fun(num1,num2); }