sunhongbo 发表于 2012-8-29 15:56:50

ACM趣题天天练 6 Box of Bricks

题目有点长,不过挺有趣的一道题,亲们,耐心看完哦。。

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题

陀枪师姐 发表于 2012-8-29 16:36:39

这是啥呀 {:6_168:}

hslx111 发表于 2012-8-29 16:49:19

这道题真正的精髓在于PE= =调了2次才过


//hdu 2088
#include<iostream>
using namespace std;

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

justcx 发表于 2012-8-29 17:01:38

#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>av) count2+=v-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了

yel_hb 发表于 2012-8-29 17:03:02

这题也够水...
#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;
                sum = 0;
                total = 0;

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

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

        return 0;
}

sunhongbo 发表于 2012-8-29 17:54:41

陀枪师姐 发表于 2012-8-29 16:36 static/image/common/back.gif
这是啥呀

亲,是ACM题,是国际大学生程序设计竞赛。英文全称是ACM International Collegiate ProgrammingContest。。

sunhongbo 发表于 2012-8-29 17:55:22

hslx111 发表于 2012-8-29 16:49 static/image/common/back.gif
这道题真正的精髓在于PE= =调了2次才过




谢谢支持。。

simon3322 发表于 2012-8-29 18:37:51

本帖最后由 simon3322 于 2012-8-29 18:39 编辑



#include "stdafx.h"
#include <iostream>

using namespace std;

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

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

vividly 发表于 2012-8-29 19:59:04

英文不好的伤不起。。。

maxOrder石 发表于 2012-8-29 21:12:15

#include<stdio.h>
#define M 50
void main()
{
int n;
int stack;

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);
                   sum+=stack;
                  
                }
                if(0!=sum%n)
                {
                  return ;
                }
                else
                {
       
                   for(i=0;i<n;i++)
                   {
                       
                      if(stack!=(sum/n))
                          {
                               
                          movies++;
                          }
                   }

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

                }
       }
}

}
页: [1]
查看完整版本: ACM趣题天天练 6 Box of Bricks