先算出每堆積木堆應該要有的高度(平均高度),接著一堆一堆下去搜尋,只要找到高度比平均高度還高的積木堆,就把超出的高度累積起來,全部掃完以後累積的數目即為所求
要注意題目有說每組測資之後要印出空行,免的像我一樣吃了個WA
#include <stdio.h>
int main(void)
{
int n, a, i, moves, set, h[100];
set = 1;
while (scanf("%d", &n) == 1 && n)
{
for (i = a = 0; i < n; i++)
{
scanf("%d", &h[i]);
a += h[i];
}
a /= n;
for (i = moves = 0; i < n; i++)
if (h[i] > a)
moves = moves + (h[i] - a);
printf("Set #%d\n", set++);
printf("The minimum number of moves is %d.\n\n", moves);
}
return 0;
}
沒有留言:
張貼留言