zoukankan      html  css  js  c++  java
  • 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



    题意:给你你也序列,要你把它分成两份,使得两份序列的和相等,求有多少种分法....


    解题思路:

     设序列的和为sum,分开后,打一个序列为sum1,第二个为sum2.  他们满足这么一个关系。  sum1+sum2=sum  则,sum1=sum2,可以表示为sum1=sum-sum1.

    所以我们就可以先再输入的时候就算出sum,然后就只要循环算sum1,加判断是否相等就好了




    代码如下:



     1 #include <stdio.h>
     2 int a[100050];
     3 int main()
     4 {
     5     int n;
     6     while(scanf("%d",&n)==1&&n){
     7         long long sum1=0,sum2=0,sum=0;
     8         int t=0;
     9         for(int i=0;i<n;i++){
    10             scanf("%d",&a[i]);
    11             sum+=a[i];
    12             }
    13         //printf("sum=%I64d
    ",sum);
    14         for(int i=0;i<n-1;i++){
    15             sum1+=a[i];
    16             //printf("sum1=%I64d sum2=%I64d
    ",sum1,sum-sum1);
    17             if(sum1==(sum-sum1)){
    18                 t++;
    19             }
    20 
    21         }
    22 
    23         printf("%d
    ",t);
    24     }
    25 }
  • 相关阅读:
    OI 知识总览 算法篇 之 动态规划
    LeetCode 16.3Sum Closest
    LeetCode 1.Two sum
    leetCode 15. 3Sum
    leetCode 54. Spiral Matrix
    mybatis(视频)
    mybatis
    spring笔记
    Spring(一)
    Spring(二)
  • 原文地址:https://www.cnblogs.com/huangguodong/p/4716147.html
Copyright © 2011-2022 走看看