Back to list of problems
Box of Bricks
591.c
#include <stdio.h> #include <stdlib.h> #include <string.h> int num; int bricks[50]; void calculate(int set) { int total=0; int i; int moves=0; for(i=0; i<num; i++) { total+=bricks[i]; } for(i=0; i<num; i++) { if (bricks[i]>(total/num)) { moves+=bricks[i]-(total/num); } } printf("Set #%d\n", set); printf("The minimum number of moves is %d.\n\n", moves); } int main(void) { int i; int set; for(set=1;;set++) { if ((fscanf(stdin, " %d", &num)!=1) || (num==0)) { exit(0); } for(i=0; i<num; i++) { if (fscanf(stdin, " %d", &bricks[i])!=1) { exit(1); } } calculate(set); } exit(0); }