题目
思路
递归,从左向右依次尝试即可。
AC代码
#include <bits/stdc++.h>
using namespace std;
int assignmentApples(int n, int index, int now) {
int res = 0;
if (now == n)
return 1;
else if (now > n)
return 0;
for (int i = index; i <= n; ++i) {
res += assignmentApples(n, i + 1, now + i);//从1开始一直向右尝试
}
return res;
}
int main() {
int n;
cin >> n;
int cnt = assignmentApples(n, 1, 0);
cout << cnt;
}
后记
最近迷上github了,等那边博客调回我喜欢的样子应该就在github上写东西了。