1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.IO;
5 using System.Text;
6 using System.Text.RegularExpressions;
7
8 namespace Timus_Online
9 {
10 class StonePiles
11 {
12 static void Main()
13 {
14 string[] input = Console.In.ReadToEnd().Split(new char[] { ' ', '\t', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
15 int N = int.Parse(input[0]);
16 int[] weights = new int[N];
17
18 for (int i = 0; i < weights.Length; ++i)
19 {
20 weights[i] = int.Parse(input[i + 1]);
21 }
22
23 int result=int.MaxValue;
24 int[] piles = new int[2];
25 for (int i = (1 << N) - 1; i >= 0; --i)
26 {
27 piles[0] = weights[N-1];
28 piles[1] = 0;
29
30 for (int j = N - 2; j >= 0; --j)
31 piles[(((i >> j) & 1) == 0) ? 1 : 0] += weights[j];
32 int temp = Math.Abs(piles[0] - piles[1]);
33 if (result > temp)
34 result = temp;
35 }
36 Console.WriteLine(result);
37 }
38 }
39 }

 

题目的石子数目为20。总的分法有2^20 = 1024*1024 ~100000种,可以枚举。

第25行是生成总的枚举数目,用二进制表示其中的0和1可表示取与不取。

posted on 2011-09-06 14:14  Mathida  阅读(161)  评论(0编辑  收藏  举报