问题:
http://acm.pku.edu.cn/JudgeOnline/problem?id=1664思路:
就是根据题意进行DFS,要注意的是如何避免重复: 所放苹果数目递减
居然一会就AC了哈哈,暑假的锻炼还是有些成果的:)
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 int n, m;
5 int total;
6
7 void
8 dfs(int level, int left, int last)
9 {
10 int i, up;
11 if(level==n) {
12 if(left == 0)
13 ++total;
14 return;
15 }
16 up = left<last ? left : last;
17 for(i=up; i>=0; i--)
18 dfs(level+1, left-i, i);
19 }
20
21 int
22 main(int argc, char **argv)
23 {
24 int tests;
25 scanf("%d", &tests);
26 while(tests--) {
27 scanf("%d %d", &m, &n);
28 total = 0;
29 dfs(0, m, m);
30 printf("%d\n", total);
31 }
32 }