题目有点长,不过挺有趣的一道题,亲们,耐心看完哦。。 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§ionid=2&problemid=8 2088题 |
[技术| 编程·课件·Linux] ACM趣题天天练 6 Box of Bricks
sunhongbo
· 发布于 2012-08-29 15:56
· 1232 次阅读
转载文章时务必注明原作者及原始链接,并注明「发表于 软院网 RuanYuan.Net 」,并不得对作品进行修改。
这道题真正的精髓在于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; } |
评分
[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了 |
评分
这题也够水... [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; } |
评分
本帖最后由 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之后终于成功了... |
评分
[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); } } } } |