第六章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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现