25-题目1437:To Fill or Not to Fill

65 篇文章 0 订阅
12 篇文章 0 订阅

http://ac.jobdu.com/problem.php?pid=1437

错误的地方:若当前价格最小(在一次能到达的最大长度内只有当前最小),则先找到这段距离的最小价格的站作为下一站的补充点,在当前站充满油,下一站再补充到满?还是怎么,这儿有问题,我不会
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string>
#include<algorithm>
#include <iomanip> //小数点对齐
using namespace std;

typedef struct GasStation
{
	float price;
	int distance;
}GasStation;
bool cmp(GasStation a, GasStation b)
{
	return a.distance < b.distance;
}

int main()
{
	int Cmax, D, Davg, N;  //Cmax为油箱最大容量,D为距离,Davg为每单位

油行驶距离, N为加油站个数
	ifstream cin("data.txt");
	while (cin >> Cmax >> D >> Davg >> N)
	{
		GasStation *gas_station = new GasStation[N];
		int i, j, next = 0;
		float cur_price = 0, cur_drive = 0, cur_gas = 0;  //当前已

花费,当前行驶长度,当前油箱容量
		for (i = 0; i < N; i++)
			cin >> gas_station[i].price >> gas_station

[i].distance;
		sort(gas_station, gas_station + N, cmp);    //按距离递增排

序

		for (i = 0; i < N; i++)
			cout << gas_station[i].price << "	" << 

gas_station[i].distance << endl;
		cout << "每次最长距离:" << Davg*Cmax << endl;

		i = 0;
		while (i < N && cur_drive < D)
		{
			//Davg*Cmax为加满油的最大行驶距离
			next = i;
			float temp = gas_station[i].price;
			for (j = i + 1; gas_station[j].distance <= 

cur_drive + Davg*Cmax && j < N; j++)  //找出在Davg*Cmax以内第一个比当前价格

小的加油站
			{
				if (temp > gas_station[j].price)
				{
					next = j;

					break;
				}
			}

			if (next == i)  //接下来的Davg*Cmax里内没有比当前价

格小的加油站,则找最小的
			{
				if (cur_drive + Davg*Cmax > D)  //最后一段
				{
					float final = D - cur_drive;
					cur_drive = D;
					cur_gas = final / Davg;
					cur_price += cur_gas * gas_station

[i].price;
					break;
				}
				temp = gas_station[i + 1].price;
				for (j = i + 1; gas_station[j].distance <= 

cur_drive + Davg*Cmax && j < N; j++)  //找出在当前加油站往目的地方向

Davg*Cmax米之内的最便宜的加油站
				{
					if (temp >= gas_station[j].price)
					{
						next = j;
						temp = gas_station

[j].price;
					}
				}
			}
			float sub = gas_station[next].distance - 

gas_station[i].distance;//下一站和当前站的距离
			cur_drive += sub;
			cur_gas = sub / Davg;
			cur_price += cur_gas * gas_station[i].price;
			i = next;

		}//end of while2

		cout << cur_price << endl;
	}//end of while1
	system("pause");
	return 0;
}

正确的代码如下
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<fstream>
using namespace std;

class Station
{
public:
	int dis;
	float pri;
	Station()
	{
		dis = 0;
		pri = 0;
	}

};

bool cmp(Station a, Station b)
{
	return a.dis < b.dis;
}

Station s[501];
float res;//结果


int main()
{

	int cmax, d, n, i, j;
	float davg;
	int maxDis;//加满一次行驶最大距离


	float res = 0;
	ifstream cin("data.txt");
	while (cin >> cmax)
	{
		res = 0;
		cin >> d >> davg >> n;

		maxDis = cmax * davg;

		for (i = 0; i<n; i++)
		{
			cin >> s[i].pri >> s[i].dis;
		}
		s[n].dis = d;//去特殊化

		sort(s, s + n, cmp);

		if (s[0].dis != 0)
		{
			cout << "The maximum travel distance = 0.00" << endl;
			continue;
		}

		int curPos = 0;
		float curOil = 0;//当前距离、油箱剩油

		for (i = 0; i<n; i++)//每次一站
		{
			curPos = s[i].dis;
			//无法到达终点
			if (curPos + maxDis < s[i + 1].dis)
			{
				cout << "The maximum travel distance = ";
				cout << setiosflags(ios::fixed) << setprecision(2) << curPos + maxDis + 0.0 << endl;
				break;
			}

			//减掉中间行驶耗油
			if (i>0)
			{
				curOil = curOil - (s[i].dis - s[i - 1].dis) / davg;
			}

			//如果最大距离内有较便宜
			int curDis = 0;
			bool isCheap = false;
			for (j = i + 1; j<n; j++)
			{
				curDis = s[j].dis - s[i].dis;
				if (curDis>maxDis)//如果没有
				{
					//res += (cmax - curOil)*s[i].pri;
					//curOil = cmax;
					break;
				}
				if (s[j].pri<s[i].pri)//有行驶到便宜站
				{
					float temp = curOil;

					curOil = (s[j].dis - s[i].dis) / davg;//应保证油量

					if (temp - curOil < 0)
					{
						res += (curOil - temp)*s[i].pri;//补充加油
					}
					else
					{
						curOil = temp;//不需要加油
					}

					isCheap = true;
					break;
				}
			}// end of for
			//最大距离内未找到便宜站
			if (!isCheap)
			{
				if (curPos + maxDis >= d)//到达终点
				{
					res += ((d - curPos) / davg - curOil) *s[i].pri;
					cout << setiosflags(ios::fixed) << setprecision(2);
					cout << res << endl;

					break;
				}
				else//未到终点,加满油
				{
					res += (cmax - curOil)*s[i].pri;
					curOil = cmax;
				}
			}
		}//end of for (每次一站)

	}//end of while
	system("pause");
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值