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

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

  • 相关阅读:
    mysql 数据类型总结
    #微信小程序子传父 #小程序子组件向父组件传值 小程序子组件触发父组件中的事件
    #最近看到了一个写的很棒的系列文章专栏
    《MySQL45讲》读书笔记(四):索引
    《MySQL45讲》读书笔记(六):数据库事务概述
    《MySQL45讲》读书笔记(一):三大日志概述
    Java基础篇(05):函数式编程概念和应用
    数据采集组件:Flume基础用法和Kafka集成
    架构设计:数据服务系统0到1落地实现方案
    Java基础篇(04):日期与时间API用法详解
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4711986.html
Copyright © 2011-2022 走看看