首页 > 其他 > 详细

uva - UVA 1388 - Graveyard (数学推理)

时间:2014-02-13 08:00:02      阅读:306      评论:0      收藏:0      [点我收藏+]

题意:有一个周长为10000的圆上等距分布着n个雕塑,现在又加入m个雕塑,位置随意,希望n+m个雕塑仍然均匀分布。这就要移动其中一些雕像,求移动的最小距离。

方法:仍然是刘大大的例题,假定某一个雕像不动,作为坐标原点,其他雕像按照逆时针标上到原点的距离标号。不是真是距离而是按比例缩小后的。接下来移动到离它最近的位置,例题用了四舍五入,感觉是不对的,例如x = 0.5, y = 1.499999,但是x和y的差还是小于1,而相邻雕像的距离才为1,所以这样看也是可行的。

注意:floor和四舍五入的写法。

#include <iostream>  
#include <iomanip>  
#include <string>  
#include <cstring>  
#include <cstdio>  
#include <queue>  
#include <stack>  
#include <algorithm>  
#include <cmath>  

using namespace std;

int main()  
{  
#ifdef Local    
	freopen("a.in", "r", stdin);    
#endif
	int n = 0, m = 0, i = 0;
	while (cin >> n >> m)
	{
		double ans = 0.0;
		for (i = 1; i < n; i++)
		{
			double pos = (double)i/n * (n+m);
			ans += fabs(pos - floor(pos+0.5)) / (n+m);
		}
		cout << setprecision(4) << fixed << ans*10000 << endl;
	}
}

要求严谨的话,应该和前后两个位置比较距离,取小的那个,这里贴上一位仁兄的代码,我就偷懒啦。

来自:http://blog.csdn.net/mr_zys/article/details/17270885

#include <stdio.h>
#include <string.h>
#include <math.h>
const double len = 10000;
int n,m;
double d0,d1;
int main()
{
  while(~scanf("%d%d",&n,&m)) {
    if(m % n == 0) printf("0.0\n");
    else {
      d0 = len / (n * 1.0);
      d1 = len / ((n + m) * 1.0);
      double ans = 0.0;
      for(int i = 1; i < n; i++) {
	double t = i * d0;
	int d = floor(t/d1);
	double t1 = fabs(t - d * d1);
	double t2 = fabs(t - (d + 1.0) * d1);
	if(t1 > t2) ans += t2;
	else ans += t1;
      }
      printf("%.4lf\n",ans);
    }
  }
  return 0;
}

bubuko.com,布布扣

uva - UVA 1388 - Graveyard (数学推理)

原文:http://blog.csdn.net/u013545222/article/details/19127397

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!