Posted on 2009-04-19 23:07
superman 阅读(102)
评论(0) 编辑 收藏 引用 所属分类:
USACO
1 #include <iostream>
2
3 using namespace std;
4
5 int n;
6 string s;
7
8 int calc()
9 {
10 int sum = 0;
11
12 int p = 0;
13 while (true)
14 {
15 int i = p + 1, cn = 0;
16 while (i < s.size() && (isdigit(s[i]) || s[i] == ' '))
17 {
18 if (isdigit(s[i]))
19 cn = cn * 10 + (s[i] - '0');
20 i++;
21 }
22
23 if (s[p] == '+')
24 sum += cn;
25 if (s[p] == '-')
26 sum -= cn;
27
28 if (i == s.size())
29 break;
30 else
31 p = i;
32 }
33
34 return sum;
35 }
36
37 void addOperator(int i, int p)
38 {
39 if (i == n - 1)
40 {
41 if (calc() == 0)
42 cout << s.c_str() + 1 << endl;
43 return;
44 }
45
46 s[p] = ' ';
47 addOperator(i + 1, p + 2);
48
49 s[p] = '+';
50 addOperator(i + 1, p + 2);
51
52 s[p] = '-';
53 addOperator(i + 1, p + 2);
54 }
55
56 int main()
57 {
58 freopen("zerosum.in", "r", stdin);
59 freopen("zerosum.out", "w", stdout);
60
61 cin >> n;
62
63 s += '+';
64 for (int i = 1; i <= n; i++)
65 {
66 s += (i + '0');
67 if (i < n)
68 s += ' ';
69 }
70
71 addOperator(0, 2);
72
73 return 0;
74 }
75