Candy I
来源:互联网 发布:回忆专用小马甲知乎 编辑:程序博客网 时间:2024/06/15 16:13
Jennifer is a teacher in the first year of a primary school. She has gone for a trip with her class today. She has taken a packet of candies for each child. Unfortunatelly, the sizes of the packets are not the same.
Jennifer is afraid that each child will want to have the biggest packet of candies and this will lead to quarrels or even fights among children. She wants to avoid this. Therefore, she has decided to open all the packets, count the candies in each packet and move some candies from bigger packets to smaller ones so that each packet will contain the same number of candies. The question is how many candies she has to move.
Input specification
The input file consists of several blocks of data. Each block starts with the number of candy packetsN(1<= N <=10000) followed by N integers (each less than 1000) in separate lines, giving the number of candies in each packet. After the last block of data there is the number -1.
Output specification
The output file should contain one line with the smallest number of moves for each block of data. One move consists of taking one candy from a packet and putting it into another one. If it is not possible to have the same number of candies in each packet, output the number -1.
Example
Input file:511116234-1Output file:4-1
巨水无比
#include<cstdio>#include<cstring>using namespace std;int a[10005];int main(){ int n; while(scanf("%d",&n)!=EOF&&n!=-1) { int sum=0,ans=0; memset(a,0,sizeof(a)); for(int i=0; i<n; i++) { scanf("%d",&a[i]); sum+=a[i]; } if(sum%n==0&&n!=0) { sum=sum/n; for(int i=0; i<n; i++) { a[i]=a[i]-sum; if(a[i]>=0) ans+=a[i]; } printf("%d\n",ans); } else printf("-1\n"); }}
- Candy I
- Candy I
- hrbust I Want Candy 2353
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- Candy
- candy
- Candy
- 3-5
- c#通过post提交给java spring boot,utf-8 % & 乱码的问题。
- 在 Ubuntu 中永久修改 USB 设备权限
- Linux服务器Cron计划任务
- 【个人Unity笔记】烘焙光照贴图后保留法线和光探头
- Candy I
- RabbitMq学习笔记五:路由选择(Routing)
- (四)Podfile文件(原文翻译)
- 4-1
- Android VideoView播放视频遇到的问题
- inBitmap的用法
- 微信开发
- 图片缓存
- Android插件化框架和热修复技术的资料收集和汇总