poj 1477 Box of Bricks

简介:

很简单的题目,我看完题到AC大概一共就7、8分钟。。。同样比赛的时候希望遇到

不过不太完美的是,我presentation error了一次,原因就是结尾还应该打一个空行。。。如果我继续这么不仔细,真的会吃大亏!吸取教训吧。。。

一道很简单的数学问题,思路是:先求平均数,然后求出每个数与平均数的差(绝对值)并累加,最后输出累加和的一半即可


AC的代码:

#include <iostream>

inline int Abs(int n){return n<0?(-n):n;}

int main()
{
	int count=0;
	int n,i;
	int a[55];
	int sum;
	int result;
	//n!=0 等价于 n
	while(scanf("%d",&n) && n)
	{
		count++;
		printf("Set #%d\n",count);

		sum=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum+=a[i];
		}

		sum/=n;
		result=0;
		for(i=1;i<=n;i++)
			result+=Abs(sum-a[i]);

		printf("The minimum number of moves is %d.\n\n",result/2);
	}

	return 0;
}


相关文章
HDOJ(HDU) 2088 Box of Bricks(平均值)
HDOJ(HDU) 2088 Box of Bricks(平均值)
83 0
HDOJ(HDU) 2088 Box of Bricks(平均值)
hdu 1312 Red and Black(BFS)
hdu 1312 Red and Black(BFS)
136 0
|
机器学习/深度学习
HDOJ/HDU 1556 Color the ball(树状数组)
HDOJ/HDU 1556 Color the ball(树状数组)
98 0
|
Java Go
POJ 1163 The Triangle
POJ 1163 The Triangle
95 0
HDOJ 1312 (POJ 1979) Red and Black
HDOJ 1312 (POJ 1979) Red and Black
107 0
|
Java 机器学习/深度学习
HDU 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18404    Accepted Submission(s...
824 0
poj-1046-color me less
Description A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform jus...
944 0
poj-1163-The Triangle
Description 73 88 1 02 7 4 44 5 2 6 5(Figure 1) Figure 1 shows a number triangle.
683 0