当前位置: 首页 > news >正文

E - 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

这道题非常简单,但是输出格式有点儿坑,尤其对于我这种才坑的人来说。在这里批评很多博客主只是为了经验!根本不管这道题的考察的是什么。
这道题注重的是输出格式。要保证输入0前没有空行,而在两组数据之间必须有空行。(想想都是气)
代码如下:(大家注意看如何保障输入0时没空行的操作)

#include<iostream>
using namespace std;
int num[51];

int main()
{
 int n;
 int t = 0;
 while (cin >> n&&n)
 {
  if (t != 0)cout << endl;
  t = 1;
  for (int i = 0; i < n; i++)
  {
   cin >> num[i];
  }
  int sum = 0;
  for (int i = 0; i < n; i++)
  {
   sum += num[i];
  }
  sum = sum / n;
  int Sum = 0;
  for (int i = 0; i < n; i++)
  {
   if (num[i]>sum)
   {
    Sum += num[i] - sum;
   }
  }
  printf("%d\n", Sum);
 }
}

转载于:https://www.cnblogs.com/damaoranran/p/8711934.html

相关文章:

  • centos安装iftop监控服务器流量
  • 【laravel5.4】重定向带参数
  • 基数排序
  • PHP array_walk() 函数
  • D、GO、Rust 谁会在未来取代 C?为什么?
  • Python基础—函数(Day9)
  • 移动开发平台 Flutter
  • Android事件分发机制详解
  • Python基础—文件操作(Day8)
  • 用编程来玩的游戏 CodeCombat 已全面开源
  • 下载编译 Android wear 源代码,尝试制作可穿戴设备功能
  • Python基础-字符串实例2
  • oracle经典书籍推荐
  • Android高仿糗事百科(含服务端)
  • MongoDB 被攻击风波未平,如何避免黑客入侵?
  • es的写入过程
  • go append函数以及写入
  • mysql 5.6 原生Online DDL解析
  • mysql外键的使用
  • Redash本地开发环境搭建
  • vue 个人积累(使用工具,组件)
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 对JS继承的一点思考
  • 回顾2016
  • 前端相关框架总和
  • 前端性能优化--懒加载和预加载
  • 三栏布局总结
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 算法-插入排序
  • 学习JavaScript数据结构与算法 — 树
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • Java总结 - String - 这篇请使劲喷我
  • 容器镜像
  • (+4)2.2UML建模图
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (剑指Offer)面试题34:丑数
  • (七)Java对象在Hibernate持久化层的状态
  • (四)c52学习之旅-流水LED灯
  • (原創) 如何將struct塞進vector? (C/C++) (STL)
  • (转载)PyTorch代码规范最佳实践和样式指南
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET Framework 4.6.2改进了WPF和安全性
  • .net on S60 ---- Net60 1.1发布 支持VS2008以及新的特性
  • .NET Standard 的管理策略
  • .net和php怎么连接,php和apache之间如何连接
  • .net连接MySQL的方法
  • .NET文档生成工具ADB使用图文教程
  • .NET中的Exception处理(C#)
  • .Net组件程序设计之线程、并发管理(一)
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka
  • @KafkaListener注解详解(一)| 常用参数详解
  • [Angular 基础] - 自定义指令,深入学习 directive