第六章9

【题目描述】

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? 

【输入】

The input consists of two lines. The first line contains 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,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.

【输出】

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.

【样例输入】

6
5 2 4 1 7 5

【样例输出】

The minimum number of moves is 5.
复制代码
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n,i,sum=0,y=0;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
        sum+=a[i];
    }
    sum/=n;
    for(i=0;i<n;i++)
    {
        if(a[i]<sum)
            y+=sum-a[i];
    }
    printf("The minimum number of moves is %d.",y);
    return 0;
}
复制代码

 

posted @   aquAAA  阅读(178)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示