zoukankan      html  css  js  c++  java
  • Box of Bricks

    此博客链接:https://www.cnblogs.com/ping2yingshi/p/12346319.html

    Box of Bricks(75min)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2088

    Problem 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 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?
     
    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, 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

    题解:

           题目意思:把几堆不同高度的砖块移成高度相同的几堆,每次只能移动一个砖块。

           思路:所有砖块加起来除以几堆砖块,得到每一堆应该放多少砖块h,把高的砖块移动到低的砖块处,满h高度不在移动。定义一个数组接收每堆砖块个数,求砖块总和除以n得到每堆应该放多高的砖块h,每堆砖块数减去h即的应该移动多少块砖,但是这是把移动的砖块算了两边,最后要除以2.

           注意:此题巨坑就是最后输出格式,最后输出格式应该是这样的(如下图)

     代码如下:

    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
        int n;
        int t=0;
        while (~scanf("%d",&n))
        {
            if (n == 0)
               break;
            if(t>0)
                 printf("
    ");    
            int c[50];
            int i = 0;
            int sum=0;
            int h=0;//每堆砖块高度 
            int count=0;//移动砖数 
            int sumcount=0;//移动总砖数 
            for(i=0;i<n-1;i++)
            { 
                scanf("%d ",&c[i]);
                sum=sum+c[i];
            } 
            scanf("%d",&c[n-1]);
            sum=sum+c[n-1]; 
            h=sum/n;
            for(i=0;i<n;i++)
            {
                count=c[i]-h; 
                if(count<0)
                    count=-count;
                sumcount=sumcount+count;
            }
            printf("%d
    ",sumcount/2);
           t++;
        }
        return 0;
    }
  • 相关阅读:
    系统维护-安全-测试等方面的开源项目
    linux命令学习
    Rancher + k8s + docker 部署资料
    个人java框架 技术分析
    java十年,需要学会的Java开发体系
    IdentityServer4 经典文章
    .net core 资料网站 和 开源项目
    Java 学习资料网站集合
    spring cloud资料汇总
    秦九韶公式 【多项式】
  • 原文地址:https://www.cnblogs.com/ping2yingshi/p/12346319.html
Copyright © 2011-2022 走看看