不是VIP会员,不能显示答案

题目解答

题目:
#include<iostream>
using namespacestd;
int g(int m, intn, int x) {
int ans = 0;
int i;
if( n == 1)
return 1;
for (i=x; i <=m/n; i++)
ans += g(m -i, n-1, i);
return ans;
}
int main() {
int t, m, n;
cin >> m >> n;
cout << g(m, n, 0) << endl;
return 0;
}
输入: 8 4
输出:15
考点: 0
分析:
解答: g(8,4,0)=g(8,3,0)+g(7,3,1)+g(6,3,2)=10+4+1=15
g(8,3,0)=g(8,2,0)+g(7,2,1)+g(6,2,2)=5+3+2=10
g(8,2,0)=g(8,1,0)+g(7,1,1)+g(6,1,2)+g(5,1,3)+g(4,1,4)=5
g(7,2,1)=g(6,1,1)+g(5,1,2)+g(4,1,3)=3
g(6,2,2)=g(4,1,2)+g(3,1,3)=2

g(7,3,1)=g(6,2,1)+g(5,2,2)=3+1=4
g(6,2,1)=g(5,1,1)+g(4,1,2)+g(3,1,3)=3
g(5,2,2)=g(3,1,2)=1
 
g(6,3,2)=g(4,2,2)=g(2,1,2)=1
评论:
老师: 0