codeforces A. Kitahara Haruki's Gift 解题报告
2024-09-28 02:25:51
题目链接: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 ;
}
最新文章
- .NetCore中的日志(1)日志组件解析
- Objective-C基础3
- 推荐10个很棒的AngularJS学习指南
- projecteuler 10001st prime (求出第10001个质数)
- 我是如何自学Android,资料分享(2015 版)
- web安全 -- 常见攻击方法及预防措施
- jquery保存用户名和密码到cookie里面
- 将集合类转换成DataTable
- Oracle 导入本地dmp文件 详细操作步骤
- 6月19日 NSFileHandle文件类的常用方法
- cocos2d-x于android在call to OpenGL ES API with no current context
- 在GridView的中有一个DropDownList,并且DropDownList有回传事件
- JavaScript创建对象的方式
- 每天学点SpringCloud(四):Feign的使用及自定义配置
- Peer Programming Project: 4 Elevators Scheduler 学号后三位 157,165
- Cocos Creator 获取节点的方式
- Codeforces821B Okabe and Banana Trees 2017-06-28 15:18 25人阅读 评论(0) 收藏
- 为 Apache 配置 UTF-8 中文编码
- CSS HTML 常用属性备忘录
- 26个Jquery1.4使用小技巧
热门文章
- [Python] &#39;unicode&#39; object is not callable
- BestCoder Round #29 1003 (hdu 5172) GTY&#39;s gay friends [线段树 判不同 预处理 好题]
- Android 网络编程之HttpURLConnection运用
- ctrl+c,ctrl+d,ctrl+z在linux程序中意义和区别
- Permission denied: user=administrator, access=WRITE, inode=";/";:root:supergroup:drwxr-xr-x
- CKeditor如何实现图片上传功能
- Java对象的死亡
- HeatMap(热图)的原理和实现
- groovy入门 第05章 基本输入输出
- 很不错的js特效