• 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
    9
    1 5 -6 7 9 -16 0 -2 2
    Output
    3
    Input
    3
    1 1 1
    Output
    0
    Input
    2
    0 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
    ");
                break;
            }
            for(int i=0;i<t-1;i++)
            {
                zot+=a[i];
                if(zot==sum/2)
                    ++flag;
            }
            printf("%d
    ",flag);
        }
        return 0;
    }
    
     
  • 相关阅读:
    [20170706]SQL Server事务复制订阅端,job不小心被删,修复
    [20170629]带过滤的复制项UI操作导致订阅全部初始化问题
    自动创建数据库镜像,证书交换
    “RESOURCE MONITOR“CPU占用特别高
    索引视图导致死锁
    Percona TokuDB
    从MySQL 5.5迁移到Mariadb 10.1.14
    SQL Server 2014新特性:其他
    SQL Server 2014新特性:分区索引重建
    SQL Server 2012 新特性:其他
  • 原文地址:https://www.cnblogs.com/xl1164191281/p/4716197.html
Copyright © 2020-2023  润新知