题目地址https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=456&problem=4134&mosmsg=Submission+received+with+ID+18707596
这两天卡在这题。多亏吧里的大神和老师呀。
个人理解如下:
floor的作用是找到离原坐标最近的整数点 若小数部分>=0.5则向上一点 反之向下取,除以(m+n)的作用是转换参考的圆为10000大小
由于以(n+m)大小的圆作为参考,每个整点是每个(m+n)上的点 故若pos四舍五入为整数,则四舍五入代表的整点是(n+m)圆上所求目标点,
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2&&n&&m)
{
double ans=0.0;
for(int i=1;i<n;i++)
{
double pos=(double)i/n*(n+m);
ans+=abs(pos-floor(pos+0.5))/(n+m);
}
printf("%.4f\n",ans*10000);
}
return 0;
}