首页 > 其他分享 >HDOJ2088 Box of Bricks

HDOJ2088 Box of Bricks

时间:2023-02-20 11:03:16浏览次数:24  
标签:Box arr int HDOJ2088 number height Bricks stacks bricks


Box of Bricks


Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18367    Accepted Submission(s): 5994


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?


HDOJ2088 Box of Bricks_System


 



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


 


这道题其实也是很坑的,也就是PE的问题,和2093 算菜价是一个道理的,都需要一个flag来标记第一个案例


打印第一个案例的时候只换行,然后之后的案例在输出之前都输出一个空行




另外,题目是让我们计算最小的移动次数,也就是一列加一,另一列减一算一次


这样,我们只算加一或减一的就OK了




下面AC代码:



import java.util.Scanner;

public class Main{
private static Scanner scanner;

public static void main(String[] args) {
scanner = new Scanner(System.in);
boolean boo = true;
while (scanner.hasNext()) {
int n = scanner.nextInt();
if (n == 0) {
break;
}
if (!boo) {
System.out.println();
}
int sum = 0;// 总和
int arr[] = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scanner.nextInt();
sum += arr[i];
}
int ave = sum / n;// 平均值
int count = 0;// 最小次数
for (int i = 0; i < arr.length; i++) {
if (arr[i] > ave) {
count += arr[i] - ave;
}
}
System.out.println(count);
boo = false;
}
}
}



标签:Box,arr,int,HDOJ2088,number,height,Bricks,stacks,bricks
From: https://blog.51cto.com/u_15741949/6067966

相关文章

  • HDOJ2143 box
    boxTimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):9731    AcceptedSubmission(s):2155Probl......
  • 基于VirutalBox搭建虚拟机间互通的可访问公网的mini主机群
    目标在单台PC机上安装3+台虚拟机[1]这些虚拟机间可以相互访问宿主机[2]与虚拟机可以相互访问虚拟机可以访问公网本文将采用NAT+Host-Only双网卡的方式实现上述......
  • 解决VirtualBox+Ubuntu20.04网络配置的问题
    1、关闭虚拟机,打开管理>>全局设定>>网络,点击右侧+就会创建一个NatNetwok,点击⚙进行设置2、进入管理>>主机网络管理器,创建适配器并启用,手动配置网卡3、DHCP设置如下,可根......
  • xshell链接virtualbox 虚拟机 CentOs7
    安装虚拟机​​点击下载​​安装xshell​​点击下载​​(填写邮箱,邮箱里下载)下载CentOs​​点击下载CentOs7​​创建虚拟机,安装刚刚下载的镜像如图设置:xshell设置:如果失败,......
  • 给gridview添加checkBox 并且做全选功能
    对gridview添加模板编辑模板-->显示中选择:HeaderTemplate-->把chekcBox拖进来                显示中选择:ItemTemplate-->把chekcBox拖进来 在headerTem......
  • VirtualBox 解决不能为虚拟电脑打开一个新任务问题
    亲测有用!!链接:https://blog.csdn.net/jf_52001760/article/details/124209728首先,管理员身份打开windows10命令行提示符。输入bcdedit并回车,找到hypervisorlaunchty......
  • inputbox-inputquery-messagedlg
    InputBoxprocedureTForm1.Button1Click(Sender:TObject);varstr:string;beginstr:=InputBox('输入窗口标题','输入提示','默认输入内容');Label4.Capti......
  • 把流氓 QQ 装进 Sanboxie-Plus
     利用开源Sandboxie-Plus为Windows桌面程序创建隔离沙箱环境守护隐私保持系统干净整洁  ......
  • lazarus combobox的下拉列表在linux时没有高亮显示选中的item
    lazarusObjectInspector和combobox的下拉列表在linux时没有高亮显示选中的item,在windows是有高亮显示的,按以下方法修改就可以,如果你有更有效的方法也请与我分享。(lazar......
  • 在不生成新的分区前提下,对Virtualbox虚拟机进行扩容
    先参考这个资料:https://www.howtogeek.com/124622/how-to-enlarge-a-virtual-machines-disk-in-virtualbox-or-vmware/ 做到gparted这步时,选择如图这个按钮,可以拉动磁盘......