题目链接 题意:题意很简单就是模拟一个过程,但是模拟很复杂错了很多次。 下面是AC代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
double d,v0,v1,v2,t;
cin>>d>>v0>>v1>>v2>>t;
double start0=0,start1=1,start2=0;
double flagt=0;
double sumt=0;
double t0=(double)d*1.0/v0*1.0;
double t1=(double)(d-1)*1.0/v1*1.0;
if(v1*t+1<d)
{
double ans;
while(1)
{
flagt=(double)(start1-start0)*1.0/(v2-v1)*1.0;
sumt+=flagt;
if(sumt>=t)
{
sumt-=flagt;
t-=sumt;
ans=start0+t*v2;
break;
}
start1+=(double)v1*1.0*flagt*1.0;
start0+=(double)v0*1.0*flagt*1.0;
flagt=(double)(start1-start0)*1.0/(v2+v0)*1.0;
sumt+=flagt;
if(sumt>=t)
{
sumt-=flagt;
t-=sumt;
ans=start1-t*v2;
break;
}
start1+=(double)flagt*v1;
start0+=(double)flagt*v0;
}
printf("%.7lf",ans);
}
else if(v1*t+1>=d&&v0*t<d)
{
double kk;
double ans;
int flag=0;
while(1)
{
flagt=(double)(start1-start0)*1.0/(v2-v1)*1.0;
sumt+=flagt;
if(sumt>=t1&&flag==0)
{
sumt-=flagt;
flagt=(d-start0)*1.0/v2*1.0;
v1=0;
flag=1;
start1=d;
sumt+=flagt;
}
if(sumt>=t)
{
sumt-=flagt;
t-=sumt;
ans=start0+t*v2;
break;
}
start1+=(double)v1*1.0*flagt*1.0;
kk=start1;
start0+=(double)v0*1.0*flagt*1.0;
flagt=(double)(start1-start0)*1.0/(v2+v0)*1.0;
sumt+=flagt;
if(sumt>=t1&&flag==0)
{
start1=d;
v1=0;
flag=1;
}
if(sumt>=t)
{
start1=kk;
sumt-=flagt;
t-=sumt;
ans=start1-t*v2;
break;
}
start1+=(double)flagt*v1;
start0+=(double)flagt*v0;
}
printf("%.7lf\n",ans);
}
else printf("%.7lf",d);
return 0;
}
注意:每个变量都要设成double形式,除了个别的,这一点很容易错
|