codeforces --- Round #248 (Div. 2) A. Kitahara Haruki's Gift
Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.
Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.
But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?
The first line contains an integer n (1 ≤ n ≤ 100) — the number of apples. The second line contains n integers w1, w2, ..., wn (wi = 100 or wi = 200), where wi is the weight of the i-th apple.
In a single line print "YES" (without the quotes) if it is possible to divide all the apples between his friends. Otherwise print "NO" (without the quotes).
3
100 200 100
YES
4
100 100 100 200
NO
In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.
【题目大意】
有两种面值的钞票,100元或200元的各有若干张,现在要将其分给两个人,要你判断能否均分。
【题目分析】
既然判断能否均分,那就来做分类讨论:
伪代码如下:
if(200元的能均分)
{
if(100元的也能均分)
输出YES;
else 输出NO;
}
else
{
if(100元的数量<2)
输出NO;
else
{
100元的数量-=2;//(为了将200元的凑成2的倍数,当然也可不减)
if(100元的数量能均分)
输出YES;
else 输出NO;
}
}
结束;
AC代码1:
#include<iostream> using namespace std; int main() { int n; cin>>n; int i,a; int cnt1=0,cnt2=0; for(i=0;i<n;i++) { cin>>a; if(a==100) cnt1++; else cnt2++; } if(cnt2%2==0) { if(cnt1%2==0) cout<<"YES"<<endl; else cout<<"NO"<<endl; } else { if(cnt1<2) cout<<"NO"<<endl; else { cnt1-=2; if(cnt1%2==0) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }
ac代码2:
#include<iostream> using namespace std; int main() { int n; cin>>n; int i,a; int cnt1=0,cnt2=0; for(i=0;i<n;i++) { cin>>a; if(a==100) cnt1++; else cnt2++; } if(cnt2%2==0) { if(cnt1%2==0) cout<<"YES"<<endl; else cout<<"NO"<<endl; } else { if(cnt1<2) cout<<"NO"<<endl; else { if(cnt1%2==0) cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }
ac代码3:
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; long n,a,h,t,i; int main() { while(~scanf("%ld",&n)) { h=t=0; for(i=0;i<n;i++) { scanf("%ld",&a); if(a==100) h++; else t++; } t=t%2; h-=t*2; if(h<0 || h%2==1) printf("NO\n"); else printf("YES\n"); } return 0; }
ac代码4:
#include <iostream> #include <stdio.h> using namespace std; int main() { int t; cin >> t; int a=0; int b=0; int e; for (int i=0;i<t;i++) { cin >> e; if (e==200)a++; else b++; } int c=0,d=0; while (a>0) { if (c<d) { c+=200; } else { d+=200; } a--; } while(b>0) { if (c<d) { c+=100; } else { d+=100; } b--; } if (c==d)cout << "YES" << endl; else cout << "NO" << endl; return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3749775.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?