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步. 思路:我们可以对二进 ...
随机推荐
- Linux大文件分割split和合并cat使用方法
本文主要介绍linux下两个命令:split和cat.其中,相信大家都熟悉cat命令,一般用来查看一个文件的内容,但是它还其它的功能,比如这里要介绍的文件合并功能,它可把多个文件内容合并到一个文件中. ...
- Python 学习笔记 - 10.类(Class) 1
定义 Python 的 Class 比较特别,和我们习惯的静态语言类型定义有很大区别. 1. 使用一个名为 __init__ 的方法来完成初始化.2. 使用一个名为 __del__ 的方法来完成类似析 ...
- Spring MVC 之请求处理方法可接收参数(三)
请求处理方法可接收参数 今天学习了前三个方法. 1.作用域对象2.单个表单提交数据3.表单数据封装的Bean对象 首先创建一个实体对象. package com.cy.springannotation ...
- Java 数据类型之间的转换 拆分字符串 Date/Calendar的转换
数据类型转换 1. String - Int String str="123"; int i=1; int str=Integer.parseInt(str); String i= ...
- PHP工程师突破
身边有几个做PHP开发的朋友,因为面试,也接触到不少的PHP工程师,他们常疑虑自己将来在技术上的成长与发展,我常给他们一些建议,希望他们能破突自己,有更好的发展. PHP工程师面临成长瓶颈 先明确我所 ...
- DedeCMSV57数据库结构文档
表名:dede_addonarticle(ENGINE=MyISAM/CHARSET=gbk) 说明:Top 字段名 说明描述 具体参数 aid 文章ID mediumint(8) unsig ...
- MySQL中基本的多表连接查询教程
一.多表连接类型1. 笛卡尔积(交叉连接) 在MySQL中可以为CROSS JOIN或者省略CROSS即JOIN,或者使用',' 如: SELECT * FROM table1 CROSS JOIN ...
- hdu 1561 The more, The Better (树上背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- nyoj------20吝啬的国度
吝啬的国度 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来.现在,Tom在第S号城市 ...
- HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)
A simple stone game ...