左右相等问题

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E

题目:

Description

Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece, and each piece contains positive integer amount of squares. Would you help Bob solve this problem?

Input

The first input line contains integer n (1 ≤ n ≤ 105) — amount of squares in the stripe. The second line contains n space-separated numbers — they are the numbers written in the squares of the stripe. These numbers are integer and do not exceed 10000 in absolute value.

Output

Output the amount of ways to cut the stripe into two non-empty pieces so that the sum of numbers from one piece is equal to the sum of numbers from the other piece. Don't forget that it's allowed to cut the stripe along the squares' borders only.

     Sample 1

Input
9
1 5 -6 7 9 -16 0 -2 2
Output
3
Sample 2
Input
3
1 1 1
Output
0
Sample 3
Input
2
0 0
Output
1

题意:
求使序列左右相等的方法个数。
分析:
将序列的和用数组sum保存起来sum[i]表示序列前i+1数的和,然后比较sum[i]与sum[n-1]-sum[i]是否相等即可。
 1 #include<iostream>
 2 using namespace std;
 3 const int maxn=100005;
 4  int a[maxn],sum[maxn];
 5  int cnt=0,i,m;
 6 int main()
 7 {
 8    int n;
 9     cin>>n>>a[0];
10     sum[0]=a[0];
11      for( i=1;i<n;i++)
12      {
13          cin>>a[i];
14          sum[i]=sum[i-1]+a[i];
15      }
16      for(i=0;i<n;i++)
17      {
18         
19        if(sum[i]==sum[n-1]-sum[i])
20        {
21             m=i;
22           if(m==n-1) break;     //排除在序列最后划分的情况(当序列总和等于0时会存在)
23              cnt++;
24        }
25      }
26        cout<<cnt<<endl;
27    return 0;
28 }

 

posted @ 2015-08-08 17:27  枫虹  阅读(255)  评论(0编辑  收藏  举报