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 分析: 直接用循环分成两半进行比较
1 #include2 using namespace std; 3 #define maxn 100000+5 4 int n, ans,flag; 5 int sum,lsum, rsum; 6 int arr[maxn]; 7 int judge(int m) 8 { 9 lsum = 0, rsum = 0;10 for (int i = 0; i > n;34 for (int i = 0; i < n; i++)35 {36 cin >> arr[i];37 sum += arr[i];38 }39 if (sum % 2 != 0)40 flag = 0; //总和为奇数,直接判断不可以分成相等的两半41 else42 judge(n);43 if (flag == 1)44 cout < << endl;45 else46 cout << "0" << endl;47 48 return 0;49 }