HDOJ(HDU) 2088 Box of Bricks(平均值)
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.
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.
Sample Input
6
5 2 4 1 7 5
0
Sample Output
5
就是输入一组数字(代表砖),让它全部只能有平均值那么高,问移动的最少次数。(保证平均数是整数)
一次只能移动一个。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=0;
while(sc.hasNext()){
int n =sc.nextInt();
if(n==0){
break;
}
if(t==0){
t=1;
}else{
System.out.println();
}
int a[] = new int[n];
long sum=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
sum+=a[i];
}
int avg = (int)sum/n;
int num=0;
for(int i=0;i<n;i++){
if(a[i]>avg){
num+=a[i]-avg;
}
}
System.out.println(num);
}
}
}
HDOJ(HDU) 2088 Box of Bricks(平均值)的更多相关文章
- HDU 2088 Box of Bricks(脑洞)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...
- HDU 2088 Box of Bricks
http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
- HDU 1326 Box of Bricks(思维)
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stac ...
- 杭电 2088 Box of Bricks
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2088 解题思路:一堆高度不同的砖块,需要把它们砌成一堵墙,即每一堆砖的高度相同(即砖的总数除以砖的堆数 ...
- HDU 1326 Box of Bricks(水~平均高度求最少移动砖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1326 题目大意: 给n堵墙,每个墙的高度不同,求最少移动多少块转使得墙的的高度相同. 解题思路: 找到 ...
- HDOJ(HDU) 2143 box(简单的多次判断-用的卫条件)
Problem Description One day, winnie received a box and a letter. In the letter, there are three inte ...
- HDOJ 1326. Box of Bricks 纯水题
Box of Bricks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1326 Box of Bricks(简单题)
Problem Description Little Bob likes playing with his box of bricks. He puts the bricks one upon ano ...
随机推荐
- Android 高仿微信实时聊天 基于百度云推送
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38799363 ,本文出自:[张鸿洋的博客] 一直在仿微信界面,今天终于有幸利用百 ...
- ajax提交富文本,内容被截断,解决方法及思路
问题描述: 使用百度的UEditor富文本插件用于前端富文本编辑,后端使用jsp,提交普通文本没有问题,后来发现在提交某些指定文本时,数据查回的数据出现不完整现象:第一件事就是想到“垃圾编辑器”
- CSS before和after伪元素
CSS中有一个特性允许我们添加额外元素而不扰乱文档本身,它们是以CSS选择器的形式出现的,具有标签的表现效果,但是呢又不是真正的标签元素,所以叫做“伪元素”.下面就说一下常见的两个伪元素before和 ...
- c# 使用递归 循环遍历导航树结构 并解析
1.数据书库结构 1 家用电器 0 一级菜单 2 手机.数码.京东通信 0 一级菜单 3 电脑.办公 0 一级菜单 4 家具.家居.厨房 0 一级菜单 5 男装.女装.童装.内衣 0 一级菜单 6 个 ...
- Css3中的响应式布局的应用
Media Queries直译过来就是“媒体查询”,在我们平时的Web页面中head部分常看到这样的一段代码: <link href="css/reset.css" rel= ...
- Open vSwitch在openstackHavana概述
最近再看Open vSwitch一些东西,我认为openstack官网上对这一块做了一些了解,所以就把这一块翻译出来以供参考,英语不好,翻译得很粗糙. Open vSwitch插件是最有名的核心插件之 ...
- 08_linux下安装chrome
首先下载chrome,需要改hosts哦(o(^▽^)o,别告诉我你不会,可以问度娘.谷哥哦) 下载地址:https://dl.google.com/linux/direct/google-chrom ...
- 2.2.2 从 Path 中获取信息
Demo: import java.nio.file.Path; import java.nio.file.Paths; public class PathInfoTest { public stat ...
- MongoDB-GRIDFS大文件系统
gridfs 是一种在mongodb中存储大二进制文件的机制,使用gridfs的原因: 1.存储巨大的文件(视频图片). 2.利用GRIDFS可以简化需求. 3.GRIDFS 利用已经建立起来的复制以 ...
- 如何让你的Python程序支持多语言
如何让你的Python程序支持多语言 本文介绍如何通过Python标准库gettext帮助你的程序支持多语言. 代码例子 import random guessesTaken = 0 print(_( ...