算法競賽入門經典 墓地雕塑
/*
Name: 墓地雕塑
Copyright: 劉汝佳
Author: Analyst
Date: 01/03/14 11:35
Description: dev-cpp 5.5.3
*/
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, m;
while (scanf("%d%d", &n, &m) == 2)
{
double ans = 0.0;
for (int i = 1; i < n; ++i)
{
double pos = (double)i / n * (n+m); //計算每個需要移動的雕塑的座標
ans += fabs(pos - floor(pos+0.5)) / (n+m); //累計移動距離
}
printf("%.4lf\n",ans*10000); //等比例擴大座標
}
return 0;
}
ps:
1.主要用到座標縮放,自己畫一個圖。
2.理解:double pos = (double)i / n * (n+m); //計算每個需要移動的雕塑的座標
3.floor() 表示向下取整。floor(x+0.5)就相當於4舍5入了。感覺和(int)(x+0.5)也差不多。