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 Input
9
1 5 -6 7 9 -16 0 -2 2
3
3
1 1 1
0
2
0 0
1
题目大意:有一个序列包含n个数,将其分为左右两部分,问要使得左右两边数之和须相等,求出现这种情况的次数。
分析:这题要求很简单,用暴力求解在简单不过了。先求这个序列的总和(用sum表示),左边数之和用z表示,
如果z=sum/2,就将次数加1(用cnt表示).
注意:由于序列的个数较大,不宜每次将z累加之后再判断其是否等于sum/2,这样所花的时间很长;只需判断sum%2的值是1还是0,
如果是1,则sum为奇数,直接输出0,如果是0,代表sum为偶数,继续累加再判断。
代码如下:
#include <iostream>
#include <cstdio>
const int maxn=100005;
using namespace std;
int main()
{
int t,sum,a[maxn],flag,zot;
while(scanf("%d",&t)==1)
{
sum=0,zot=0,flag=0;
for(int i=0;i<t;i++)
{
scanf("%d",a+i);
sum+=a[i];
}
if(sum%2)
{
printf("0
");
break;
}
for(int i=0;i<t-1;i++)
{
zot+=a[i];
if(zot==sum/2)
++flag;
}
printf("%d
",flag);
}
return 0;
}