uva 591 - Box of Bricks

这题比较简单,加和求出平均数,然后多余平均数的砖块数量和就是最少要移动的。

 

/*
 * uva591.cpp
 *
 *  Created on: 2013-4-17
 *      Author: kevinjiang
 */
#include<cstdio>

int bricks[55];

int main() {
	//setbuf(stdout,NULL);
	int n;
	int counter=1;
	while (scanf("%d", &n) != EOF) {
		if (n == 0)
			break;
		int sum = 0, avg, move = 0;
		for (int i = 0; i < n; i++) {
			scanf("%d", &bricks[i]);
			sum += bricks[i];
		}
		avg = sum / n;

		for (int i = 0; i < n; i++) {
			if (bricks[i] > avg) {
				move += bricks[i] - avg;
			}
		}
		printf("Set #%d\nThe minimum number of moves is %d.\n\n",counter++,move);

	}
	return 0;
}

 

你可能感兴趣的:(uva)