zoukankan      html  css  js  c++  java
  • Codeforces 18C C. Stripe

    Codeforces 18C  C. Stripe

    链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/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.求前K个数的和b[k]和后n-k+1个数的和c[k+1]

    2.比较大小,若相等记一次

    代码:

     1 #include<cstdio>
     2 #include<iostream>
     3 using namespace std;
     4 const int maxn=100001;
     5 
     6 int a[maxn],b[maxn],c[maxn]; //数组很大时要放在外面,不然会出现WA
     7     
     8 int main()
     9 {
    10     int n;
    11     scanf("%d",&n);
    12     b[0]=0;
    13     for(int i=1;i<=n;i++)
    14     {
    15         scanf("%d",&a[i]);
    16         b[i]=b[i-1]+a[i];    //求前i项的和
    17     }
    18     for(int j=n;j>=1;j--)
    19     {
    20         c[j]=c[j+1]+a[j];   //求后n-j+1项的和
    21     }
    22     int ans=0;
    23     for(int k=1;k<n;k++)
    24     {
    25         if(b[k]==c[k+1])  //比较
    26             ans++;
    27     }
    28     printf("%d
    ",ans);
    29     return 0;
    30 }
    View Code

    读了好久的题目,一开始对负数那里不理解,仔细分析了一下,又听别人说了就理解了。

  • 相关阅读:
    Excel中substitute替换函数的使用方法
    如何在Excel中提取小数点后面的数字?
    提升单元测试体验的利器--Mockito使用总结
    SpringMVC项目读取不到外部CSS文件的解决办法及总结
    java8 Lambda表达式的新手上车指南(1)--基础语法和函数式接口
    Spring-data-redis操作redis知识总结
    优雅高效的MyBatis-Plus工具快速入门使用
    Thrift入门初探(2)--thrift基础知识详解
    Thrift入门初探--thrift安装及java入门实例
    spring事件驱动模型--观察者模式在spring中的应用
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4711986.html
Copyright © 2011-2022 走看看