[技术| 编程·课件·Linux] ACM趣题天天练 6 Box of Bricks

sunhongbo · 发布于 2012-08-29 15:56 · 1230 次阅读
41
题目有点长,不过挺有趣的一道题,亲们,耐心看完哦。。

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
Set #1
The minimum number of moves is 5.

源自 杭电ACM http://acm.hdu.edu.cn/game/entry/problem/show.phpchapterid=1&sectionid=2&problemid=8 2088题

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
共收到 9 条回复
陀枪师姐 · #2 · 2012-8-29 16:36:39  回复 支持 反对
这是啥呀

点评

亲,是ACM题,是国际大学生程序设计竞赛。英文全称是ACM International Collegiate ProgrammingContest。。  详情 回复 发表于 2012-8-29 17:54
hslx111 · #3 · 2012-8-29 16:49:19  回复 支持 反对
这道题真正的精髓在于PE= =调了2次才过

[C++] 纯文本查看 复制代码
//hdu 2088
#include<iostream>
using namespace std;

int main()
{
    int n,h[55];
    bool flag=false;
    while(cin>>n&&n!=0)
    {        
        int i,sum=0,mov=0;
        for(i=0;i<n;i++)
        {
            cin>>h[i];
            sum+=h[i];
        }
        sum=sum/n;
        for(i=0;i<n;i++)
        {
            if(h[i]>sum)
                mov+=(h[i]-sum);
        }
        if(flag)  cout<<endl;
        cout<<mov<<endl;
        flag=true;
    }
    return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

点评

谢谢支持。。  详情 回复 发表于 2012-8-29 17:55

评分

参与人数 1学分 +6 收起 理由
sunhongbo + 6

查看全部评分

justcx · #4 · 2012-8-29 17:01:38  回复 支持 反对
[C++] 纯文本查看 复制代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int>v;
int n,sum,av,count2,b;
int count1=0;
int sign=0;
while(cin>>n)
{ if(n==0)
break;
count1++;
sum=0;
count2=0;
v.clear();
for(int i=0;i<n;i++)
{
cin>>b;
v.push_back(b);
sum+=b;
}
av=sum/n;
for(int j=0;j<v.size();j++)
{
if(v[j]>av) count2+=v[j]-av;
}
if(sign==1)
cout<<endl;
//cout<<"Set #"<<count1<<endl;
//cout<<"The minimum number of moves is "<<count2<<"."<<endl;
cout<<count2<<endl;
sign=1;
}
return 0;
}

用最后的平均高度减去所以比它低的高度,再把所有结果加起来就OK了

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 1学分 +6 收起 理由
sunhongbo + 6 地板哦。。

查看全部评分

yel_hb · #5 · 2012-8-29 17:03:02  回复 支持 反对
这题也够水...
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;

int main()
{
	int n,k,i,ave,sum,total;
	int *stacks;

	k = 1;
	while(cin >> n && n != 0)
	{
		stacks = new int[n];
		sum = 0;
		total = 0;

		for(i = 0; i < n; ++i)
		{
			cin >> stacks[i];
			sum += stacks[i];
		}
		
		ave = sum / n;

		for(i = 0; i < n; ++i)
		{
			if(stacks[i] > ave)
			{
				total += (stacks[i] - ave);
			}
		}
		
		cout << "Set #" << k++ << endl;
		cout << "The minimum number of moves is " << total << "." << endl << endl;
	}

	return 0;
}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 1学分 +6 收起 理由
sunhongbo + 6 好,下次争取不水哈。。

查看全部评分

sunhongbo · #6 · 2012-8-29 17:54:41  回复 支持 反对
陀枪师姐 发表于 2012-8-29 16:36
这是啥呀

亲,是ACM题,是国际大学生程序设计竞赛。英文全称是ACM International Collegiate ProgrammingContest。。
sunhongbo · #7 · 2012-8-29 17:55:22  回复 支持 反对
hslx111 发表于 2012-8-29 16:49
这道题真正的精髓在于PE= =调了2次才过

[mw_shl_code=cpp,true]

谢谢支持。。
simon3322 · #8 · 2012-8-29 18:37:51  回复 支持 反对
本帖最后由 simon3322 于 2012-8-29 18:39 编辑



[C++] 纯文本查看 复制代码
#include "stdafx.h"
#include <iostream>

using namespace std;

void main()
{
        int n;
        int bricknum[50];
        int j = 0;
        while( cin>>n && n != 0 )
        {
                int sum = 0;
                for(int i=0;i<n;i++)
                {
                        cin>>bricknum[i];
                        sum += bricknum[i];
                }
                sum=sum/n;
                int move = 0;
                for(int i=0;i<n;i++)
                {
                        if(bricknum[i]>sum)
                        {
                                move += bricknum[i] - sum;
                        }
                }                
                if ( j == 1 )cout<<endl;
                cout<<move<<endl;
                j=1;
        }
}


经过两次PE之后终于成功了...

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

评分

参与人数 1学分 +6 收起 理由
sunhongbo + 6 好。。

查看全部评分

vividly · #9 · 2012-8-29 19:59:04  回复 支持 反对
英文不好的伤不起。。。
maxOrder石 · #10 · 2012-8-29 21:12:15  回复 支持 反对
[C] 纯文本查看 复制代码
#include<stdio.h>
#define M 50
void main()
{
  int n;
  int stack[M];
 
  int id=0;
  while(1)
  {
	   int movies=0;
	   int sum=0;
	   id++;
     scanf("%d",&n);
     if(0==n)
	 {
	   return;
	 }
	 else
	 {
		
		int i;

       
		getchar();
	    for(i=0;i<n;i++)
		{
		 scanf("%d",&stack[i]);
		   sum+=stack[i];
		  
		}
		if(0!=sum%n)
		{
		  return ;
		}
		else
		{
	
		   for(i=0;i<n;i++)
		   {
			 
		      if(stack[i]!=(sum/n))
			  {
				 
			    movies++;
			  }
		   }

		   printf("Set #%d\n",id);
           printf("The minimum number of moves is %d\n",movies);

		}
	 }
  }

}

评分

参与人数 1学分 +6 收起 理由
sunhongbo + 6

查看全部评分

回帖
B Color Image Link Quote Code Smilies
Command + Enter
快速回复 返回顶部 返回列表