Program E-- CodeForces 18C

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

Input

91 5 -6 7 9 -16 0 -2 2

Output

3

Input

31 1 1

Output

0

Input

20 0

Output

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\n");
            break;
        }
        for(int i=0;i<t-1;i++)
        {
            zot+=a[i];
            if(zot==sum/2)
                ++flag;
        }
        printf("%d\n",flag);
    }
    return 0;
}
 
时间: 2024-10-12 22:14:47

Program E-- CodeForces 18C的相关文章

CodeForces 18C - Stripe 解题心得

原题: 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

CodeForces 18C

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

Codeforces Round #174 (Div. 2)---D. Cow Program(dp, 记忆化搜索)

Farmer John has just given the cows a program to play with! The program contains two integer variables, x and y, and performs the following operations on a sequence a1,?a2,?-,?an of positive integers: Initially, x?=?1 and y?=?0. If, after any step, x

CodeForces 468A Program F

Description Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game. Initially you have a sequence of n integers: 1, 2, ..., n. In a single step, you can pick two of them, let's denote

Codeforces 283B Cow Program

题意:给你a[2] - a[n], 一个初始x = 1,初始y = 0  ,执行下面步骤 1) x +=a[x] ,y += a[x] 2) x -= a[x] ,y += a[x] 3)重复1-2步骤.只要中间x <=0 || x > n  就跳出.输出y的值 问你a[1] 从 [1,n-1]分别的值为多少. 解题思路:dfs 求 dp[i][0/1]  求以加法进入 i 和以减法进入 i 能得到的值. 解题代码: 1 // File Name: 283b.cpp 2 // Author:

CodeForces 540C Program D

Description You play a computer game. Your character stands on some level of a multilevel ice cave. In order to move on forward, you need to descend one level lower and the only way to do this is to fall through the ice. The level of the cave where y

CodeForces 534D Program B

Description On February, 30th n students came in the Center for Training Olympiad Programmers (CTOP) of the Berland State University. They came one by one, one after another. Each of them went in, and before sitting down at his desk, greeted with tho

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store

Codeforces Round #339 (Div. 2) B. Gena&#39;s Code

B. Gena's Code It's the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena's code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, f