代码呢
时间: 2023-07-02 13:24:01 浏览: 55
抱歉,我忘记附上代码了。下面是对应的 C++ 代码实现:
```c++
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
using namespace std;
struct GasStation {
double distance; // 距离起点的距离
double price; // 油价
};
bool cmp(const GasStation &a, const GasStation &b) {
return a.distance < b.distance;
}
int main() {
double A, B, C, D;
int n;
cin >> A >> B >> C >> D >> n;
vector<GasStation> stations(n);
for (int i = 0; i < n; i++) {
cin >> stations[i].distance >> stations[i].price;
}
stations.push_back({A, 0}); // 将终点作为一个加油站
sort(stations.begin(), stations.end(), cmp); // 按距离从小到大排序
double curDistance = 0; // 当前位置距离起点的距离
double curGas = 0; // 当前汽油量
double cost = 0; // 费用
int i = 0; // 下一个加油站的下标
while (curDistance < A) { // 如果还没到达终点
double nextDistance = curDistance + B * C; // 下一个加油站的距离
double minPrice = D; // 最低油价
int minPriceIndex = -1; // 最低油价对应的加油站下标
for (; i < n && stations[i].distance <= nextDistance; i++) { // 在范围内找最低油价
if (stations[i].price < minPrice) {
minPrice = stations[i].price;
minPriceIndex = i;
}
}
if (minPriceIndex == -1) { // 没找到符合条件的加油站
cout << "No Solution" << endl;
return 0;
}
double needGas = (stations[minPriceIndex].distance - curDistance) / C; // 到达下一个加油站需要的汽油量
if (needGas > curGas) { // 如果当前油量不足
cost += (needGas - curGas) * stations[minPriceIndex].price; // 加油
curGas = needGas;
}
curGas -= needGas; // 行驶到下一个加油站
curDistance = stations[minPriceIndex].distance;
}
cout << fixed << setprecision(2) << cost << endl; // 输出结果
return 0;
}
```
希望能对您有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)