九度1437 不知道哪错了,请求指点
/*
题目描述:
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
输入:
For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.
输出:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
样例输入:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
*/
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct gasta{
double p;//该加油站的油价
int dis;//该加油站距离杭州的距离
bool operator < (const gasta &A) const{
return dis<A.dis;
}
}sta[501];
int main(){
int Cmax,D,Davg,N;
while(scanf("%d%d%d%d",&Cmax,&D,&Davg,&N)!=EOF){
for(int i=0;i<N;i++){
scanf("%lf%d",&sta[i].p,&sta[i].dis);
}
sta[N].dis=D;//设置终点站
sta[N].p=0;
sort(sta,sta+N);
if(sta[0].dis>0){
printf("The maximum travel distance = 0.00\n");
continue;
}
int longd=Cmax*Davg;//加满油能行驶的距离
int leftD=0;//该车还能行驶的距离
double sum=0;//总共花去多少钱
bool flag=true;//能否到达
for(int i=0;i<N;i++){//达到当前加油站
if(i==0){
leftD=0;
}else{
leftD-=sta[i].dis-sta[i-1].dis;
}
if(longd+sta[i].dis<sta[i+1].dis){//判断从当前站能否到达下一站
double X=1.0*sta[i].dis+longd;
printf("The maximum travel distance = %.2lf\n",X);
flag=false;
break;
}
int min=i;//油价最低的站的编号
if(leftD<sta[i+1].dis-sta[i].dis){//决定在该站是否需要加油
for(int j=i;j<=N;j++){//决定在该站需要加多少油
if(sta[j].dis<longd+sta[i].dis && sta[min].p>sta[j].p){
min=j;
break;
}
}
if(min==i){//加满
sum+=sta[i].p*(Cmax-1.0*leftD/Davg);
leftD=longd;
}else{//只需加油到下一个加油站的量为止
double tmp=sta[min].dis-sta[i].dis-leftD*1.0;
if (fabs(tmp)>1e-5 && tmp>0){//这么做是为了防止可能出现的负数
sum+=tmp/Davg*sta[i].p;
leftD=sta[min].dis-sta[i].dis;
}
}
}
}
if(flag==true){
printf("%.2lf\n",sum);
}
}
return 0;
}
谢谢!
题目描述:
With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.
输入:
For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.
输出:
For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.
样例输入:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
50 1300 12 2
7.10 0
7.00 600
样例输出:
749.17
The maximum travel distance = 1200.00
*/
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
struct gasta{
double p;//该加油站的油价
int dis;//该加油站距离杭州的距离
bool operator < (const gasta &A) const{
return dis<A.dis;
}
}sta[501];
int main(){
int Cmax,D,Davg,N;
while(scanf("%d%d%d%d",&Cmax,&D,&Davg,&N)!=EOF){
for(int i=0;i<N;i++){
scanf("%lf%d",&sta[i].p,&sta[i].dis);
}
sta[N].dis=D;//设置终点站
sta[N].p=0;
sort(sta,sta+N);
if(sta[0].dis>0){
printf("The maximum travel distance = 0.00\n");
continue;
}
int longd=Cmax*Davg;//加满油能行驶的距离
int leftD=0;//该车还能行驶的距离
double sum=0;//总共花去多少钱
bool flag=true;//能否到达
for(int i=0;i<N;i++){//达到当前加油站
if(i==0){
leftD=0;
}else{
leftD-=sta[i].dis-sta[i-1].dis;
}
if(longd+sta[i].dis<sta[i+1].dis){//判断从当前站能否到达下一站
double X=1.0*sta[i].dis+longd;
printf("The maximum travel distance = %.2lf\n",X);
flag=false;
break;
}
int min=i;//油价最低的站的编号
if(leftD<sta[i+1].dis-sta[i].dis){//决定在该站是否需要加油
for(int j=i;j<=N;j++){//决定在该站需要加多少油
if(sta[j].dis<longd+sta[i].dis && sta[min].p>sta[j].p){
min=j;
break;
}
}
if(min==i){//加满
sum+=sta[i].p*(Cmax-1.0*leftD/Davg);
leftD=longd;
}else{//只需加油到下一个加油站的量为止
double tmp=sta[min].dis-sta[i].dis-leftD*1.0;
if (fabs(tmp)>1e-5 && tmp>0){//这么做是为了防止可能出现的负数
sum+=tmp/Davg*sta[i].p;
leftD=sta[min].dis-sta[i].dis;
}
}
}
}
if(flag==true){
printf("%.2lf\n",sum);
}
}
return 0;
}
谢谢!
...全文
相关推荐
9000元定制的三维九度分销新玩法源码
【三维九度分销新玩法源码】是一种在电商和网络营销领域常见的销售策略,它结合了三维营销和九度分销的概念,旨在通过多层次的分销体系来扩大销售网络,提高产品的覆盖范围,并激励分销商积极参与销售活动。
2019最新三维九度分销源码下载
【标题】"2019最新三维九度分销源码下载"所涉及的知识点主要集中在电商领域的分销系统和源码开发上。
Thinkphp三维九度分销新玩法微信三三复制直销系统源码.zip
【三维九度分销模式】“三维九度”分销模式是一种营销策略,具体指的是每个用户可以推荐三位新用户,这三位新用户又各自推荐三位,以此类推,形成九层的分销网络。
九度1004Median
ZJU考研机试真题 九度1004Median
九度淘宝直通车点击软件 v3.3
九度介绍九度搜索引擎点击软件是针对搜索引擎的 点击类软件软件。软件可像真人点击一样,自动点击百度、谷歌、360搜索、搜狗、搜搜、淘宝、天猫等搜索引擎内的网址,点击逼真,效果好。软件采用互刷机 制,由众


新手乐园
