POJ 1477 && HDU 1326 Box of Bricks(水~)

来源:互联网 发布:华为网络 编辑:程序博客网 时间:2024/06/06 20:23

Description
给出n个数据砌墙,n个测试数据分别代表现在的墙高度,求将墙修改为等高度最少要移动的砖块数目,且已知一定能够砌成等高的
Input
多组数据,每组数据包括墙的数量n和n堵墙的高度hi(1<=n<=50,1<=hi<=100),以n=0结束输入
Output
对于每组用例,输出使得n堵墙等高所需移动的最少砖块数目
注意:输出每组样例后输出一个空行
Sample Input
6
5 2 4 1 7 5
0
Sample Output
Set #1
The minimum number of moves is 5.

Solution
水题,先求墙高的平均值,再和墙高一一对比,只移动高出部分即为最少移动数
Code

#include<stdio.h>int main(){    int i,n,b[1000],res=1,count,ave;    while(1)    {        scanf("%d",&n);        if(n==0)            break;        printf("Set #%d\n",res++);//按格式输出         count=0;        ave=0;        for(i=0;i<n;i++)//累加         {            scanf("%d",&b[i]);            ave+=b[i];        }        ave/=n;//均值         for(i=0;i<n;i++)//移动高出部分             if(b[i]>ave)                count+=(b[i]-ave);        printf("The minimum number of moves is %d.\n\n",count);//按格式输出     } } 
0 0
原创粉丝点击