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

题意:有一个周长为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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值