NYOJ325Zb的生日

 1 #include<stdio.h>
2 #include<math.h>
3
4 int w[25], min, ncases, total, i;
5 void dfs(int cur, int sum, int a[])
6 {
7 int t;
8 if(cur == ncases) return ;
9 t =(int)fabs(total-sum-sum);
10 if(t < min) min = t;
11 dfs(cur+1, sum, a);
12 dfs(cur+1, sum+a[cur], a);
13 }
14
15 int main()
16 {
17 while(scanf("%d", &ncases) != EOF)
18 {
19 total = 0;
20 for(i=0; i<ncases; i++)
21 {
22 scanf("%d", &w[i]);
23 total += w[i];
24 }
25 min = 200001;
26 dfs(0, 0, w);
27 printf("%d\n",min);
28 }
29 return 0;
30 }

你可能感兴趣的:(OJ)