1033 To Fill or Not to Fill

代码如下:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 using namespace std;
 5 
 6 typedef struct
 7 {
 8     double pos;
 9     double price;
10 }gasstation;
11 gasstation gasst[502];
12 
13 bool cmp(gasstation a,gasstation b)
14 {
15     if(a.pos<b.pos)
16         return true;
17     return false;
18 }
19 
20 int main()
21 {
22     double Cmax,D,Davg;
23     int N;
24     scanf("%lf%lf%lf%d",&Cmax,&D,&Davg,&N);
25     int i;
26     for(i=0;i<N;i++)
27         scanf("%lf%lf",&gasst[i].price,&gasst[i].pos);
28     sort(gasst,gasst+N,cmp);
29     if(D==0)
30     {
31         printf("0.00\n");
32         return 0;
33     }
34     if(gasst[0].pos!=0)
35     {
36         printf("The maximum travel distance = 0.00\n");
37         return 0;
38     }
39     int curstnum=0;               //当前所处的油站编号,即当前的位置
40     double curgas=0;              //当前的油量
41     double curcost=0;                //当前的花费
42     bool flag=false;              //是否达到目的
43     double maxrundis=Cmax*Davg;        //邮箱加满最远能行驶的距离
44     while(!flag)
45     {
46         bool tag=false;            //最大距离内是否有加油站
47         bool ifcheaper=false;    //是否有便宜的
48         double cheapestprice=10000;    //找出最便宜的
49         int cheapestnum;        //没有更便宜的情况下,找出最便宜的
50         for(i=curstnum+1;i<N;i++)
51         {
52             if((gasst[i].pos-gasst[curstnum].pos)<=maxrundis)    //范围内
53             {
54                 tag=true;         //有加油站
55                 if(gasst[i].price<gasst[curstnum].price)        //情况3-a
56                 {                                            //且有更便宜的
57                     ifcheaper=true;
58                     double dist=gasst[i].pos-gasst[curstnum].pos;
59                     double needgas=dist/Davg-curgas;
60                     curgas=0;
61                     curcost+=(needgas*gasst[curstnum].price);
62                     curstnum=i;
63                     break;
64                 }
65                 if(gasst[i].price<cheapestprice)
66                 {
67                     cheapestprice=gasst[i].price;
68                     cheapestnum=i;
69                 }
70             }
71             else
72                 break;
73         }
74         if(!ifcheaper&&(maxrundis>=(D-gasst[curstnum].pos)))   //说明已经可以到达目的地了,情况1
75         {
76             double dist=D-gasst[curstnum].pos;
77             double needgas=dist/Davg-curgas;
78             curcost+=needgas*gasst[curstnum].price;
79             printf("%.2lf\n",curcost);
80             return 0;
81         }
82         if(tag&&!ifcheaper)            //情况3-b
83         {
84             double needgas=Cmax-curgas;
85             curcost+=(needgas*gasst[curstnum].price);
86             double dist=gasst[cheapestnum].pos-gasst[curstnum].pos;
87             curgas=Cmax-dist/Davg;
88             curstnum=cheapestnum;
89         }
90         else if(!tag)                        //情况2
91         {
92             printf("The maximum travel distance = %.2lf\n",gasst[curstnum].pos+maxrundis);
93             return 0;
94         }
95     }
96     return 0;
97 }

 

转载于:https://www.cnblogs.com/huhusw/p/9755283.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值