POJ 3154/模拟(最小墓地移动长度)

原来有a个坑,现在要添加b个坑,前后每个坑的间距都要是一样的,环形圈的长度为10000,求最小的坑移动长度

原来肯定有一个坑不动,剩下a-1个坑,a+b个坑的平均间距为average,对于这a-1个坑,如果它的坐标x满足:average*j<=x<=average*(j+1),则min(x-average*j,average*(j+1)-x)则为此坑移动距离

Sample Input
sample input #1
2 1

sample input #2
2 3

sample input #3
3 1

sample input #4
10 10

Sample Output
sample output #1
1666.6667

sample output #2
1000.0

sample output #3
1666.6667

sample output #4
0.0

#include "stdio.h"
int main()
{

	int before,after;
	double be,af,sum=0.0;
	scanf("%d%d",&before,&after);
	be=(double)10000/before;
	af=(double)10000/(before+after);
	int j=1;
	for(int i=1;i<before;i++)
	{
		
		double p1,p2;
		p1=i*be;
		while(1)
		{
		
			p2=j*af;
			if(p2<=p1&&p1<=p2+af)break;
			j++;
		}
		if(p1-p2<p2+af-p1)sum+=p1-p2;
		else sum+=p2+af-p1;
	}
	if((int)sum==sum)printf("%d.0",(int)sum);
	else
	printf("%.4lf",sum);
	return 0;
}
posted on 2011-06-04 11:32  yangyh  阅读(409)  评论(0)    收藏  举报