• Greedy method:

condition 1: you can't start the engine, there is no gas station at the beginning
condition 2: you can't arrive at the end, means there are two neiboring gas staion ar> e too far away from each other (the distance > max tank capacity * average distance > per unit gasoline)
condition 3: when you arrive at a gas station, you need to check all the other station> s you can reach from where you are now one by one,

(i)once you could find a cheaper one (not the cheapest one!!!), fiil the tank so that you can reach there and run out of all the gasoline.

(ii)if you can't find a cheaper one, just fill up the tank.

  • Code:

//  Copyright © 2018 Fangzhou Ai. All rights reserved.

//  Copying it directly is cheating if this is your homework

#include

int max_capacity;

int distance;

float avg_distance;

int total_number;

float gas_station[501][2];

float money;

void sortseq(void);//sort by distance

float greedy(void);

int main()

{

    int i;

    //input

    scanf("%d%d%f%d",&max_capacity,&distance,&avg_distance,&total_number);

    for(i=0;i

        scanf("%f%f",&gas_station[i][0],&gas_station[i][1]);



    //sort the sequence order by distance

    sortseq();

    if(gas_station[0][1]>0||total_number<1)

    {printf("The maximum travel distance = 0.00");return0;}



    for(i=0;i

    {

        if(gas_station[i+1][1]-gas_station[i][1]>max_capacity*avg_distance)

            break;

    }

    //check if it could reach the destination, if it can't, calculate the max distance

    if(gas_station[i+1][1]-gas_station[i][1]>max_capacity*avg_distance||distance-gas_station[total_number-1][1]>max_capacity*avg_distance)

    {

        printf("The maximum travel distance = %.2f",gas_station[i][1]+max_capacity*avg_distance);

    }

    else

    {

        //greedy method

        // set the destination as the cheapest gsa station to simplify the procedure

        gas_station[total_number][0]=-1;

        gas_station[total_number][1]=distance;

        money=greedy();

        printf("%.2f",money);


    }

    return0;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,458评论 0 10
  • 第八章静 静,由“青+争”组成。 青,“造字本义:从矿井采掘的苔色矿石”——《象形字典》,可以引申为“青色的”,青...
    王韶斌阅读 253评论 0 0
  • 书名:你的生命有什么可能 作者:古典 金句:成功是一个重要的人生目标,但是如果你找到“世俗成功”背后指向的“成就感...
    我是annie阅读 179评论 0 0
  • 我们工作和生活中都不免与人打交道,好的交流和沟通方式总是能让我们事倍功半,但是事实总是,我们的沟通只能让事...
    碎碎念的又又阅读 169评论 2 1