Box of bricks minimum number of moving bricks

Source: Internet
Author: User

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 he older sister Alice. "Nah, should make all stacks the same height. Then you would has a real wall. ", she retorts. After a little consideration, Bob sees that she was right. So he sets out to rearrange the bricks, and one by one, such and all stacks is the same height afterwards. But since Bob was the lazy he wants to does this with the minimum number of bricks moved. Can help?

Input

The input consists of several data sets. Each set begins with a line containing the number N of Stacks Bob have 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 is divisible by the number of stacks. Thus, it's always possible to rearrange the bricks such, and all stacks has the same height.

The input is terminated by a set starting with n = 0. This set should is processed.

Output

For each set, print the minimum number of bricks, which has the to is moved in order to make all the stacks the same height.
Output a blank line between each set.

Sample Input
65 2 4 1 7 50
Sample Output
5

#include <stdio.h>voidSortint*a,intn);inta[Wuyi];voidMain () {intN; while(SCANF ("%d", &n) = =1&& n!=0) { inti,j=0, arev=0; for(i=0; i<n;i++) {scanf ("%d",&A[i]); Arev+=A[i]; } Arev=arev/N; Sort (a,n); while(a[n-1]>Arev) { if(a[n-1]-arev>arev-a[0]) {J+ = (arev-a[0]); A[n-1]=a[n-1]-(arev-a[0]); a[0]=Arev; } Else if(a[n-1]-arev==arev-a[0]) {J+ = (arev-a[0]); A[n-1]=Arev; a[0]=Arev; } Else{J+ = (a[n-1]-Arev); a[0]=+a[0]+a[n-1]-Arev; A[n-1]=Arev; } sort (a,n); } printf ("%d\n\n", J); }}voidSortint*a,intN) { inti,j; for(i=0; i<n-1; i++) for(j=i+1; j<n;j++) { if(a[i]>A[j]) { inttemp=A[i]; A[i]=A[j]; A[J]=temp; } }}

Box of bricks minimum number of moving bricks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.