Problem Description: a car can travel NKM when it is filled with oil . There are several petrol stations on the road. An effective algorithm is designed to indicate which gas stations should be used to refuel and make the least number of refueling routes.
Algorithm Design: Calculates the minimum number of refueling times for a given n and K gas station locations.
data input:n: Indicates that the car can travel NKM after filling with oil
K: There are K gas stations on the road
K+1 integer: Represents the distance between the K gas station and the k-1 petrol station. the first 0 petrol stations indicate the departure point and the car is fully oiled. the first k+1 gas station indicates the destination.
Data output: The minimum number of refueling times and specific at which several gas stations refueling.
Example: n=7 k=7
k+1 integers:1 2 3 4 5 1 6 6
Best Value:4
Problem Analysis: Add distance, judge whether it is greater than n, if greater than N, Count once
#include <iostream>using namespacestd;intMain () {intn,k; cout<<"input N,k"<<Endl; CIN>>n>>K; inta[ -]; cout<<"Input Distance"<<Endl; for(intI=1; i<=k+1; i++) {cin>>A[i]; } intCount=0; intsum=0; for(intI=1; i<=k+1; i++) {sum+=A[i]; if(sum>=n) {sum=A[i]; Count++; cout<<"======="<<Endl; cout<<"Section"<<count <<"the location of the second refueling:"<<a[i-1]<<Endl; }} cout<<"Total Refueling:"<<count<<"Times"<<Endl; return 0;}
Auto refueling problem--greedy algorithm