ZCMU-1415

本文介绍了一个编程问题,即如何通过移动最少数量的砖块来使所有砖堆的高度相等。文章提供了一段C语言代码示例,展示了如何计算达到统一高度所需的最小移动次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1415: Box of Bricks

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 222   Solved: 101
[ 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 con- sideration, 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, first print the number of the set, as shown in the sample output. Then 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. 

Output a blank line after each set.

Sample Input

6
5 2 4 1 7 5
0

Sample Output

Set #1
The minimum number of moves is 5.

【解析】
这道题呢其实就是给你砖头数问你要移动多少块,就是说你先算出平均的高度,这个时候我们模拟一下会发现其实就是把比平均高的给移动走就存在这样的过程比平均高度高的移动移动走的砖头有的其实就是给了比平均高度矮的,有的是直接移动走了。
#include <stdio.h>
int main()
{
    int n,i,count1=0,sum1,average,sum2;
    int a[100];
 while(scanf("%d", &n)&&n!=0)
 {
      sum1=sum2= 0;
      count1++;
     for(i=0; i<n; i++)
  {
       scanf("%d", &a[i]);
  }
  for(i=0; i<n; i++)
  {
        sum1+=a[i];     //计算总数
  }
  average=sum1/n;
  for(i=0; i<n; i++)   //计算要移动的箱子数
  {
      if(a[i]>average)
   {
       sum2=sum2+a[i]-average;
   }

  }
printf("Set #%d\n", count1);
  printf("The minimum number of moves is %d.\n\n", sum2);
 }

    return 0;
}


<think>好的,用户想查询ZCMU OJ 1318的题目详情或解题方法。首先,我需要确认自己是否了解这个题目。由于ZCMU OJ的题目可能不在我的训练数据中,我需要根据现有的引用信息进行推断,或者给出一般性的建议。 查看用户提供的引用,发现引用1提到了1614题涉及字符串替换和大小写处理,引用2是1489题关于数气球,引用3是1900题关于并查集的应用。这些信息可能帮助用户解决类似问题,但用户明确询问的是1318题,因此需要处理这个具体的问题。 首先,我需要检查是否有关于1318题的引用,但用户给出的引用中并没有直接提到1318。因此,可能需要建议用户通过题目编号查找或根据常见题型推测。例如,ZCMU的题目可能涉及数据结构、算法或字符串处理等。考虑到引用中的其他题目涉及字符串处理和并查集,1318可能属于类似类型。 接下来,我应该建议用户检查题目链接或提供更多细节,比如题目描述或输入输出样例,以便更准确地分析。同时,可以给出常见的解题思路,比如字符串处理、动态规划或图论算法,并参考现有引用中的类似题目,如字符串处理要注意大小写(引用1),数气球可能涉及计数或哈希表(引用2),并查集用于处理连通性问题(引用3)。 如果用户无法提供更多信息,可以建议搜索ZCMU OJ的在线资源或论坛,比如官方网站、GitHub题解或相关讨论区。此外,提供通用的解题步骤,如分析输入输出、确定算法、处理边界情况等,可能对用户有帮助。 最后,生成相关问题,引导用户进一步提问或提供更多细节,以便更精准地解答。</think>根据现有资料,ZCMU OJ 1318的具体题目内容未在提供的引用中直接提及。以下是帮助您解决此类编程题目的通用方法及建议: ### 常见解题思路(适用于ZCMU OJ题型) 1. **字符串处理** 若涉及字符串操作(如引用[^1]中1614题的替换和大小写处理): ```python # 示例:字符串替换时保留大小写标记 original = "HelloWorld" positions = [0 if c.isupper() else 1 for c in original] # 记录大小写位置 modified = original.lower().replace("world", "Python") result = "".join([c.upper() if positions[i]==0 else c for i,c in enumerate(modified)]) ``` 2. **并查集应用** 若涉及连通性问题(如引用[^3]中1900题的多米诺骨牌): ```python class UnionFind: def __init__(self, n): self.parent = list(range(n+1)) def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): self.parent[self.find(y)] = self.find(x) ``` 3. **计数与统计** 若涉及数值统计(如引用[^2]中1489题的气球计数): ```python from collections import defaultdict def count_balloons(arr): counter = defaultdict(int) for num in arr: counter[num] += 1 return max(counter.values()) if counter else 0 ``` ### 建议步骤 1. **获取题目描述** 访问ZCMU OJ官网或通过题目编号搜索获取1318题的输入输出样例及具体要求。 2. **分析问题类型** - 若涉及字符串:注意大小写处理、特殊符号过滤 - 若涉及数学:推导公式或使用动态规划 - 若涉及图论:构建邻接表或应用DFS/BFS 3. **参考相似题目** 如引用的字符串替换题需额外记录大小写位置,这种技巧可推广到其他需要保留原始格式的场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值