题解:P15639 [ICPC 2022 Tehran R] Flower Festival
Wang_Kevin · · 题解
一道基础的模拟题。
由于需要求出最先到达玫瑰广场的汽车的编号,那么我们不妨求一下每辆汽车到达玫瑰广场所需的时间,并输出所需时间最短的车的编号。
代码如下:
#include <bits/stdc++.h>
using namespace std;
int n,f,x,v,id = 0,ans = 1e9;
int main(){
cin >> n >> f;
for (int i = 0;i < n;i++){
cin >> x >> v;
if ((f - x) / v < ans){
ans = (f - x) / v;
id = i + 1;
}
}
cout << id;
}
但是这份代码无法通过本题。
为什么呢?因为到达的时间有可能不为整数。
故需要使用 double 类型。
代码如下:
#include <bits/stdc++.h>
using namespace std;
int n,f,x,v,id = 0;
double ans = 1e9;
int main(){
cin >> n >> f;
for (int i = 0;i < n;i++){
cin >> x >> v;
if (double(double(f - x) / double(v)) < ans){
ans = double(double(f - x) / double(v));
id = i + 1;
}
}
cout << id;
}