Hdu 2088之解题报告
来源:互联网 发布:大连交通大学软件学院 编辑:程序博客网 时间:2024/06/06 08:33
Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. “Look, I've built a wall!”, he tells his older sister Alice. “Nah, you should make all stacks the same height. Then you would have a real wall.”, she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1≤n≤50 and 1≤hi≤100.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output
For each set, print the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line between each set.
Output a blank line between each set.
Sample Input
65 2 4 1 7 50
Sample Output
5
代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;bool cmp(int a,int b){ return a>b?true:false;}int main(void){ int n; int a[60]; int f =0; while(scanf("%d",&n),n) { int sum = 0; for(int i=1;i<=n;++i){ scanf("%d",&a[i]); sum += a[i]; } if(f) printf("\n"); //格式问题 f = 1; int averager = sum/n; sort(a+1,a+1+n,cmp); int position; for(int i=1;i<=n;++i) if(a[i]<=averager){ position = (i-1); break; } int minStep = 0; for(int i=1;i<=position;++i) //注意等于的情况 minStep += (a[i]-averager); printf("%d\n",minStep); } return 0;}
0 0
- Hdu 2088之解题报告
- Hdu 2036 之解题报告
- Hdu 2108 之解题报告
- Hdu 2034 之解题报告
- Hdu 2037 之解题报告
- Hdu 2045之解题报告
- Hdu 2047之解题报告
- Hdu 2100之解题报告
- Hdu 2099之解题报告
- Hdu 2096之解题报告
- Hdu 2095之解题报告
- Hdu 2094之解题报告
- 解题报告 之 HDU 4405 Aeroplane chess
- 解题报告 之 HDU 4089 Activation
- hdu oj 2044 小蜜蜂之解题报告
- Hdu 2041(水题)之解题报告
- Hdu 2042(水题)之解题报告
- HDU 3342 解题报告
- 身份证号码验证正则表达式
- Android Jni中JNI_OnLoad的地位和使用
- 参数绑定
- myeclipse中java文件头注释格式设置
- Android验证码倒计时
- Hdu 2088之解题报告
- 使用电信e8-c家庭网关时,无线路由器的设置方法
- 类风湿病友日常饮食注意事项
- Android导入Android OpenCV Samples出错
- android时间对话框TimePickerDialog介绍
- 关于ios8以后BitCode一词,关于在xcode7(bate3)中报错的解决以及普及
- windows update 失败无法进入windows解决方案
- jquery-ui日历插件的使用步骤
- Windows 下安装MongoDB