自由落体
2026-07-13 23:33:02
发布于:吉林
5阅读
0回复
0点赞
1.特判 H-K ≤ 0:小车比天花板高,下落最小时间为 0;
2.区间左右两端各扩张 eps=1e-4,匹配题目 “距离≤0.0001 即接住” 规则,解决浮点边界漏算;
3.统一误差偏移,避免因浮点微小偏差把刚好命中的球漏掉。
代码如下:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-4;
double H, S1, V, L, K, t_min, t_max, low, high;
int n, l, r, res;
int main(){
cin >> H >> S1 >> V >> L >> K >> n;
if (H - K <= eps){
t_min = 0.0;
}
else{
t_min = sqrt((H - K) / 5.0);
}
t_max = sqrt(H / 5.0);
low = S1 - V * t_max - eps;
high = S1 - V * t_min + L + eps;
l = ceil(low);
r = floor(high);
l = max(l, 0);
r = min(r, n - 1);
res = 0;
if (r >= l){
res = r - l + 1;
}
cout << res << endl;
return 0;
}
这里空空如也



有帮助,赞一个