类型:贪心题目
技巧:用floor,核心代码如下
<一>使用技巧时
while(scanf("%d %d",&n,&m)==2)
{
if(m%n==0){printf("0.0\n");continue;}
double ans=0.0;
for(int i=1;i<n;i++)
{
double now=(double)i/n*(n+m);
ans+=fabs(now-floor(now+0.5))/(n+m);
}
ans*=tot;
printf("%.4lf\n",ans);
}
<二>没使用技巧时
while(scanf("%d %d",&n,&m)==2)
{
if(m%n==0){printf("0.0\n");continue;}
double fen=tot/(n+m);
double want=0;
double ans=0.0;
for(int i=1;i<n;i++)
{
double now=tot/n*i;
while(want<now)want+=fen;
double a1=want-now;
double a2=want-fen-now;
if(a1<0)a1*=-1;
if(a2<0)a2*=-1;
ans+=min(a1,a2);
}
printf("%.4lf\n",ans);
}