m >= n, 即球数大于箱子数,分为两种箱子满,箱子不满 f[n][m] = f[n-m][m] + f[n-1][m]
箱子满 f[n-m][m]
至少有一个箱子不满f[n-1][m]
代码实现:
#include<iostream>usingnamespace std;intf(int n,int m){if(n ==0|| n ==1|| m ==1){return1;}if(n < m){returnf(n, n);}returnf(n - m, m)+f(n -1, m);}intmain(){int n =0, m =0;
cout <<"please input the number of ball and box:"<< endl;
cin >> n >> m;int ans =f(n, m);
cout <<"ans = "<< ans << endl;return0;}