1177: 零起点学算法84——Box of Bricks

1177: 零起点学算法84——Box of Bricks

Time Limit: 1 Sec  Memory Limit: 32 MB   64bit IO Format: %lld
Submitted: 769  Accepted: 268
[Submit][Status][Web Board]

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

Source

零起点学算法

 1 #include<stdio.h>
 2 int main(){
 3     int n,a[50];
 4     while(scanf("%d",&n)!=EOF&&n!=0){
 5         int s=0;
 6         for(int i=0;i<n;i++){
 7             scanf("%d",&a[i]);
 8             s+=a[i];
 9         }
10         int ave,k=0;
11         ave=s/n;
12         for(int i=0;i<n;i++){
13             if(a[i]>ave) k+=a[i]-ave;
14         }
15         printf("%d\n\n",k);
16     }
17     return 0;
18 }

Output a blank line between each set. 输出每组之间的空行。 不懂英文真坑爹。

时间: 2024-07-31 02:42:30

1177: 零起点学算法84——Box of Bricks的相关文章

1169: 零起点学算法76——绝对公正的裁判

1169: 零起点学算法76--绝对公正的裁判 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 510  Accepted: 336[Submit][Status][Web Board] Description 大家知道我们学校的OnlineJudge吗?,你知道他会告诉你什么呢? Compiling : 您提交的代码正在被编译.Running : 您的程序正在OJ上运行.Judging : OJ

1165: 零起点学算法72——首字母变大写

1165: 零起点学算法72--首字母变大写 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 705  Accepted: 439[Submit][Status][Web Board] Description 输入一个英文句子,将每个单词的第一个字母改成大写字母. Input 输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行. Output 请输出按照要求改写后的英文句

1127: 零起点学算法34——继续求多项式

1127: 零起点学算法34--继续求多项式 Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 3481  Accepted: 1985[Submit][Status][Web Board] Description 输入1个正整数n, 计算1+(1+2)+(1+2+3)+...+(1+2+3+...+n) Input 输入正整数n(多组数据) Output 输出1+(1+2)+(1+2+3)+...+

1128: 零起点学算法35——再求多项式(含浮点)

1128: 零起点学算法35--再求多项式(含浮点) Time Limit: 1 Sec  Memory Limit: 64 MB   64bit IO Format: %lldSubmitted: 2141  Accepted: 1002[Submit][Status][Web Board] Description 输入一个整数n,计算 1+1/(1-3)+1/(1-3+5)+...+1/(1-3+5-...+2n-1)的值 Input 输入一个整数n(多组数据) Output 出1+1/(1

1097:零起点学算法04——再模仿一个算术题

1097: 零起点学算法04--再模仿一个算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 2627  Accepted: 2202[Submit][Status][Web Board] Description 上题会模仿了吧.再来模仿一个. 现在要求你模仿一个乘法的算术题 Input 没有输入 Output 输出9乘以10的值 Sample Output 90 Source 零起点学算法

1098: 零起点学算法05——除法算术题

1098: 零起点学算法05--除法算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 2346  Accepted: 1932[Submit][Status][Web Board] Description 乘法会了,除法也一样的.不要跟我说不会哦. Input 没有输入 Output 输出12除以2的值,计算让计算机去做哦 Sample Output 6 Source 零起点学算法 1 #i

1099:零起点学算法06——再来一题除法算术题

1099: 零起点学算法06--再来一题除法算术题 Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 4811  Accepted: 1917[Submit][Status][Web Board] Description 再来一题除法算术题 Input 没有输入 Output 输出8除以5,保留1位小数 Sample Output 1.6 Source 零起点学算法 1 # include <std

1101: 零起点学算法08——简单的输入和计算(a+b)

1101: 零起点学算法08--简单的输入和计算(a+b) Time Limit: 1 Sec  Memory Limit: 128 MB   64bit IO Format: %lldSubmitted: 3669  Accepted: 1997[Submit][Status][Web Board] Description 前面7道题做下来,对输出和计算有点感觉了吧? 不过很可惜的是前面的做法,好像太死了,写了一个计算3+4的程序,计算5+6又得改程序,计算机真的只能这么实现,那么我们比计算机

1102: 零起点学算法09——继续练习简单的输入和计算(a-b)

1102: 零起点学算法09--继续练习简单的输入和计算(a-b) Time Limit: 1 Sec  Memory Limit: 520 MB   64bit IO Format: %lldSubmitted: 2810  Accepted: 2161[Submit][Status][Web Board] Description 简单吧,不用多说了 Input 输入2个整数a,b,用空格隔开 Output 输出a-b的值 Sample Input 10 5 Sample Output 5 S