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
9
1 5 -6 7 9 -16 0 -2 2
3
3
1 1 1
0
2
0 0
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\n");
break;
}
for(int i=0;i<t-1;i++)
{
zot+=a[i];
if(zot==sum/2)
++flag;
}
printf("%d\n",flag);
}
return 0;
}
Program E-- CodeForces 18C的更多相关文章
- Codeforces 18C C. Stripe
Codeforces 18C C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...
- CodeForces 18C
Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In eac ...
- [Benchmark] Codeflaws: A Programming Competition Benchmark for Evaluating Automated Program Repair Tools
Basic Information Publication: ICSE'17 Authors: Shin Hwei Tan, Jooyong Yi, Yulis, Sergey Mechtaev, A ...
- Codeforces Round #443 (Div. 1) A. Short Program
A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...
- Codeforces Round #879 (Div. 2) C. Short Program
题目链接:http://codeforces.com/contest/879/problem/C C. Short Program time limit per test2 seconds memor ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #443 (Div. 2) C. Short Program
C. Short Program time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 879C/878A - Short Program
传送门:http://codeforces.com/contest/879/problem/C 本题是一个位运算问题——位运算的等价变换. 假设位运算符“&”“|”“^”是左结合的,且优先级相 ...
- codeforces 284 D. Cow Program(记忆化搜索)
题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...
- Codeforces 878A - Short Program(位运算)
原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...
随机推荐
- Handler详解系列(四)——利用Handler在主线程与子线程之间互发消息,handler详解
MainActivity如下: package cc.c; import android.app.Activity; import android.os.Bundle; import android. ...
- ios中javascript直接调用oc代码而非通过改变url回调方式(转)
之前一个ios项目中,需要通过UIWebview来打开一个静态页面,并在静态页面中 调用相关object-c代码. 一.以前使用js调用object-c的方法 关于如何使用javascript调用ob ...
- google_apactest_round_A_problem_D
先尝试过小数据 题目 有8张卡牌,每个卡牌都可以有不同的等级,每个卡牌的不同等级具有不同的攻击力,可以通过花钱给卡牌充值从而升级,且每次只能升一级,比如可以花1个硬币将卡牌2从1级升级到2级,同时卡牌 ...
- commonJS — 日期操作(for Date)
for Date github: https://github.com/laixiangran/commonJS/blob/master/src/forDate.js 代码 /** * Created ...
- entity reference在views中的运用
一个views block可以获取从url过来的nid,某个node的有entity reference字段, 填入一点数据,然后在views里关联一下这个entity reference field ...
- mysql下sql语句 update 字段=字段+字符串
mysql下sql语句 update 字段=字段+字符串 mysql下sql语句令某字段值等于原值加上一个字符串 update 表明 SET 字段= 'feifei' || 字段; (postgr ...
- Jenkins运行完Test后,把ngreport生成的测试报告 拷贝到相应的文件夹
F:cd F:\program\apache-tomcat-7.0.67\webapps\Set currentPath=F:\program\apache-tomcat-7.0.67\webapps ...
- php面向对象Object
1.创建类 class 类名{ private 私有变量 只能本类的内部使用 protected 受保护的变量 本类和子类的内部 public 公开的变量 都可以使用 一般属性都设为私有 一般函数都是 ...
- 用过的正则(更新ing)
http://www.debuggex.com/ 这个很好用20120912 //十六进制颜色值的正则表达式 var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6} ...
- 工作日志 jquery slideDown slideUp
jquery里面使用 slideDown 和 slideUp会有一个像素的偏差