题目链接:http://codeforces.com/problemset/problem/433/A

题目意思:给定 n 个只由100和200组成的数,问能不能分成均等的两份。

题目其实不难,要考虑清楚数量问题即可。就是说,200的数量是奇数或偶数,100的数量是奇数或偶数时的处理。

一开始可能思路有点混乱,学人3分钟打一道题,wa了3次。

由于写得比较混乱,我的代码不好意思贴出来,以下借鉴了别人的两种好的写法。

Time: 31ms  Memory: 0KB

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
int tmp, c1, c2;
c1 = c2 = ;
for (int i = ; i < n; i++)
{
scanf("%d", &tmp);
if (tmp == )
c1++; // c1: 100的数量
else
c2++; // c2: 200的数量
}
c2 %= ; // c2: 0 or 1
c1 -= c2 * ; // 一张c2 = 二张c1
if (c1 < || c1 & ) // c1不是偶数张(不能均分)或者c1只有0张,但c2是奇数张
printf("NO\n");
else
printf("YES\n");
}
}

以下这个方法更加直接,不过一定要先排序,还有就是先从200的数量开始分配。

好厉害的写法!!!

Time:15ms  Memory: 0KB

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn]; int main()
{
int n, tmp;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
int fir = , sec = ; // fir:第一个人分到的apple grams sec:第二个人分到的
for (int i = n-; i >= ; i--)
{
if (fir < sec)
fir += a[i];
else
sec += a[i];
}
printf("%s\n", fir == sec ? "YES" : "NO");
}
return ;
}

最新文章

  1. .NetCore中的日志(1)日志组件解析
  2. Objective-C基础3
  3. 推荐10个很棒的AngularJS学习指南
  4. projecteuler 10001st prime (求出第10001个质数)
  5. 我是如何自学Android,资料分享(2015 版)
  6. web安全 -- 常见攻击方法及预防措施
  7. jquery保存用户名和密码到cookie里面
  8. 将集合类转换成DataTable
  9. Oracle 导入本地dmp文件 详细操作步骤
  10. 6月19日 NSFileHandle文件类的常用方法
  11. cocos2d-x于android在call to OpenGL ES API with no current context
  12. 在GridView的中有一个DropDownList,并且DropDownList有回传事件
  13. JavaScript创建对象的方式
  14. 每天学点SpringCloud(四):Feign的使用及自定义配置
  15. Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165
  16. Cocos Creator 获取节点的方式
  17. Codeforces821B Okabe and Banana Trees 2017-06-28 15:18 25人阅读 评论(0) 收藏
  18. 为 Apache 配置 UTF-8 中文编码
  19. CSS HTML 常用属性备忘录
  20. 26个Jquery1.4使用小技巧

热门文章

  1. [Python] &#39;unicode&#39; object is not callable
  2. BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]
  3. Android 网络编程之HttpURLConnection运用
  4. ctrl+c,ctrl+d,ctrl+z在linux程序中意义和区别
  5. Permission denied: user=administrator, access=WRITE, inode=&quot;/&quot;:root:supergroup:drwxr-xr-x
  6. CKeditor如何实现图片上传功能
  7. Java对象的死亡
  8. HeatMap(热图)的原理和实现
  9. groovy入门 第05章 基本输入输出
  10. 很不错的js特效