*寒假水64——Box of Bricks

2023-12-19 13:50
文章标签 64 寒假 box bricks

本文主要是介绍*寒假水64——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 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? 

InputThe 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. 
OutputFor 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

 

#include<iostream>
#include<string.h>using namespace std;int main()
{int n,i,k=0,sum,xin,ans;int a[101];while(cin>>n&&n!=0){memset(a,0,sizeof(a));sum=0,ans=0;for(i=0;i<n;i++){cin>>a[i];sum+=a[i];}  xin=sum/n;for(i=0;i<n;i++){if(a[i]>xin){ans+=(a[i]-xin);}}	    if(k==0){cout<<ans<<endl;}else{cout<<endl<<ans<<endl;}k++;}return 0;
}

题解:水题~

这篇关于*寒假水64——Box of Bricks的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/512425

相关文章

论文翻译:ICLR-2024 PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS

PROVING TEST SET CONTAMINATION IN BLACK BOX LANGUAGE MODELS https://openreview.net/forum?id=KS8mIvetg2 验证测试集污染在黑盒语言模型中 文章目录 验证测试集污染在黑盒语言模型中摘要1 引言 摘要 大型语言模型是在大量互联网数据上训练的,这引发了人们的担忧和猜测,即它们可能已

NGINX轻松管理10万长连接 --- 基于2GB内存的CentOS 6.5 x86-64

转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=190176&id=4234854 一 前言 当管理大量连接时,特别是只有少量活跃连接,NGINX有比较好的CPU和RAM利用率,如今是多终端保持在线的时代,更能让NGINX发挥这个优点。本文做一个简单测试,NGINX在一个普通PC虚拟机上维护100k的HTTP

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划

【Python从入门到进阶】64、Pandas如何实现数据的Concat合并

接上篇《63.Pandas如何实现数据的Merge》 上一篇我们学习了Pandas如何实现数据的Merge,本篇我们来继续学习Pandas如何实现数据的Concat合并。 一、引言 在数据处理过程中,经常需要将多个数据集合并为一个统一的数据集,以便进行进一步的分析或建模。这种需求在多种场景下都非常常见,比如合并不同来源的数据集以获取更全面的信息、将时间序列数据按时间顺序拼接起来以观察长期趋势等

2014级寒假特训之并查集专题

Problem A: Double和XXZ的生日宴请 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 9   Solved: 7 [ Submit][ Status][ Web Board] [ Edit] [ TestData] Description Double 和 XXZ同一天生日,他们俩30岁生日那天,当年

Beyond Compare4.2.4 64位OS最新密钥

亲测可用,拿来主义 6TTCoWi2N0Pv+o2HGfqUpZfuaMhtf2zX0u1OuNeqTYkKKWh-CKwBWkPUG3+CiAQ2q4MNPbf0t8+gmPdo+Vyw64aU-zuQQt9d7Q6EcJ+T42by0E+kxf+q3QLs40H+RD3h5OLjFGpxClodRnTCNoAM39xsWm2aHZI0Z9KdXzLo1fo1OdNlaptoK17SsxNK-

leetcode : 64 最小路径和 动态规划

64. 最小路径和 题目链接https://leetcode.cn/problems/minimum-path-sum/ 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。 说明:每次只能向下或者向右移动一步。 示例: [1,3,1][1,5,1][4,2,1] 输出: 7 解释: 因为路径 1→3→1

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

《Linux运维总结:基于X86_64+ARM64架构CPU使用docker-compose一键离线部署consul 1.18.1容器版分布式ACL集群》

总结:整理不易,如果对你有帮助,可否点赞关注一下? 更多详细内容请参考:《Linux运维篇:Linux系统运维指南》 一、部署背景 由于业务系统的特殊性,我们需要面向不通的客户安装我们的业务系统,而作为基础组件中的consul 针对不同的客户环境需要多次部署集群,作为一个运维工程师,提升工作效率也是工作中的重要一环。所以我觉得有必要针对 x86_64 + ARM64 CPU架构cons

Alibaba Cloud Linux 3.2104 LTS 64位 安装php8

目录 前言1、首先,更新系统的软件包索引:2、安装必要的软件包以添加更多软件,包括EPEL和Remi存储库:3、启用PHP 8.0的Remi仓库:4、安装PHP 8.0:5、安装PHP 8.0的扩展(根据需求安装):6、安装完成后,确认PHP版本:7、启动PHP-FPM并设置开机自启:总结 前言 安装PHP 8.0的步骤如下: 1、首先,更新系统的软件包索引: sud