zoukankan      html  css  js  c++  java
  • 2015 HUAS Summer Contest#3~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 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
    解题思路:这个题目的意思就是求一组数前后两端的和一样的情况有多少种,这里要注意的是它只能分成两个部分。看懂题目很重要,其实首先我就看错题了,我以为是求当两端相等时是
    在第几个数的后面,这是很容易出错的。解题步骤很简单,就是先将这组数的和求出来,然后从左网友一一遍历,遇到前面的和等于总和减去前面的和的就将要输出的数加1,但在开始前要
    讲要输出的数记为0.这里还要注意的一点是遍历只能到n-1,是不能够遍历到最后一个数的。
    程序代码:
    #include<cstdio>
    #include<iostream>
    using namespace std;
    const int m=100010;
    int a[m];
    int c,sum;
    int main()
    {
        int n,i;
    	scanf("%d",&n);
    	for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum=sum+a[i];
        }
        int floag=0;
        for(i=1;i<n;i++)
        {
            c+=a[i];
            if(c==(sum-c)) floag++;
        }
    
        printf("%d
    ",floag);
    	return 0;
    }
  • 相关阅读:
    ReactNative typescript路径别名配置,vscode不识别@/youpath配置方法
    代码片段:js数组对象排序
    ScreenToGif SharpDx 下载失败问题
    springboot的@CrossOrigin注解解决细粒度的配置跨域
    java 连接Kafka报错java.nio.channels.ClosedChannelExcep
    zookeeper日常报错总结
    Spring Aop、拦截器、过滤器的区别
    搭建zookeeper单机版以及简单命令的使用
    java后台代码发送邮件
    java后台调用http请求
  • 原文地址:https://www.cnblogs.com/chenchunhui/p/4713241.html
Copyright © 2011-2022 走看看