首页 > 其他 > 详细

591 Box of Bricks

时间:2014-03-08 17:50:05      阅读:620      评论:0      收藏:0      [点我收藏+]

真心好水的题。。。。 只需要求出砖块数目的平均值 ,再用每一摞砖的数目减去平均值 ,只取相减后大于0的或者只取小于零的相加 得到的和就是最少次数

注意输出两个回车 题目及AC代码如下

  Box of Bricks 

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 con- sideration, 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?

 

bubuko.com,布布扣

 

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 bubuko.com,布布扣 and bubuko.com,布布扣.

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, first print the number of the set, as shown in the sample output. Then print the line ``The minimum number of moves is k.‘‘, where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.

Output a blank line after each set.

 

Sample Input 

6
5 2 4 1 7 5
0

 

Sample Output 

Set #1
The minimum number of moves is 5.


bubuko.com,布布扣
 1 // 591.cpp : 定义控制台应用程序的入口点。
 2 //
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 
 7 
 8 int main(void)
 9 {
10     int n;
11     int arr[55];
12     int sum;
13     int count = 1;
14     while (scanf("%d", &n) && n)
15     {
16         memset(arr, 0, sizeof(arr));
17         sum = 0;
18         for (int i = 0; i < n; i++)
19         {
20             scanf("%d", &arr[i]);
21             sum += arr[i];
22         }
23         sum = sum / n;
24         int times = 0;
25         for (int i = 0; i < n; i++)
26         {
27             if (arr[i] - sum>0) times += (arr[i] - sum);
28         }
29         printf("Set #%d\n", count++);
30         printf("The minimum number of moves is %d.\n\n", times);
31     }
32     return 0;
33 }
34 
35                 
bubuko.com,布布扣

591 Box of Bricks,布布扣,bubuko.com

591 Box of Bricks

原文:http://www.cnblogs.com/VOID-133/p/3588046.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!