POJ-3154-Graveyard

题目:POJ-3154-Graveyard

一个周长为10000的圆圈,一开始等距的安放着N个雕塑,现在想增加M个雕塑,使得雕塑之间还是等距,问坟墓最少移动的距离。

思路:只有n个时设周长为1,则坐标为i/n,加m后周长扩至n+m{好处是新坐标为整数,0~n+m-1},新坐标为i/n*(m+n)的四舍五入,作差累加即得移动距离。

 1 #include <iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<algorithm>
 6 #include<math.h>
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int n,m,i;
13     double pos,ans;
14     while(scanf("%d%d",&n,&m)!=EOF)
15     {
16         ans=0;
17         for(i=1;i<=n-1;i++)
18         {
19             pos=(double)i/n*(n+m); //#
20             ans+=fabs(pos-floor(pos+0.5))/(n+m); //#
21         }
22         ans*=10000;
23         printf("%.4lf\n",ans);
24     }
25     return 0;
26 }

 

posted @ 2015-01-23 12:08  alohagin  阅读(165)  评论(0编辑  收藏  举报