Greedy algorithm of refueling problem __ algorithm

Source: Internet
Author: User

Car refueling problem "a car can travel n kilometres after filling it with oil. There are K gas stations on the trip. To make the least amount of refueling along the way, design an effective algorithm that points out where to refuel at those gas stations.   Data input: The input data is given by the file input.txt. The first line has 2 positive integers n and K, which means that the car can travel NKM after filling with oil, and there are K gas stations on the road. In the next 1 lines, there is a k+1 integer representing the distance between the K gas station and the first K-1 gas station. The No. 0 gas station said the departure, the car has been filled with oil. The first K+1 gas station represents the destination. Result output: The minimum number of refueling times calculated by the program is output to the file ouput.txt. If the destination cannot be reached, the "No Solution" is output. Input file Sample               output File Sample Input.txt                    output.txt             &NBS P                4 3 4 5 1 6 6             requirements: using greed algorithm, programmed to calculate and output the minimum number of refueling times, and pointed out that the gas stations should be docked refueling.

#include <stdio.h> int main () {file *fp;//pointer int a[100],b[100];
    int i,n,k,flag,sum,count,temp;
    Fp=fopen ("Input.txt", "R"); if (fp==null) {printf ("The file cannot be opened.)
        \ n ");
    Exit (0);
    } flag=0;
    i=0;
    Temp=1;
        while (!feof (FP)) {if (flag==0) fscanf (FP, "%d%d", &n,&k);//read from file N,k flag=1; if (flag!=0) {fscanf (FP, "%d", &a[i++])//read from the file the distance between the gas stations if (A[I]&GT;N)//IF
            The distance between two stations is greater than N, then the destination temp=0 cannot be reached;
    } fclose (FP);
        if (temp==0) {Fp=fopen ("Output.txt", "w");
        fprintf (FP, "No Solution");
        Fclose (FP);
    return 0;
    } count=0;
    sum=0;
        for (i=0; i<k+1; i++) {sum+=a[i];
            if (sum>n) {i--;
            b[count++]=i+1;
        sum=0;
    } fp=fopen ("Output.txt", "w");
    fprintf (FP, "%d", count);//write the data to the file fclose (FP); REturn 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.