【HDOJ】2079 选课时间

显然是个背包问题,简单题。

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 typedef struct {
 6     int cent;
 7     int num;
 8 } cent_st;
 9 
10 cent_st cents[9];
11 int n, case_n, kn;
12 
13 int f(int index, int n) {
14     int i, j, k, tmp, cnt=0;
15 
16     if (n == 0)
17         return 1;
18 
19     for (j=index; j<kn; ++j) {
20         k = cents[j].num;
21         tmp = 0;
22         while ( cents[j].num ) {
23             tmp += cents[j].cent;
24             if (tmp <= n) {
25                 cents[j].num--;
26                 cnt += f(j+1, n-tmp);
27             } else
28                 break;
29         }
30         cents[j].num = k;
31     }
32 
33     return cnt;
34 }
35 
36 int main() {
37     int i, cnt;
38 
39     scanf("%d", &case_n);
40 
41     while (case_n--) {
42         scanf("%d %d", &n, &kn);
43 
44         for (i=0; i<kn; ++i)
45             scanf("%d %d", &cents[i].cent, &cents[i].num);
46 
47         cnt = f(0, n);
48 
49         printf("%d\n", cnt);
50     }
51 
52     return 0;
53 }

 

posted on 2014-04-13 01:06  Bombe  阅读(186)  评论(0编辑  收藏  举报

导航