Analysis of algorithms vehicle refueling

The meaning of problems

 

Fill up the car can travel n kilometers. There are a number of gas stations in the journey. Design an efficient algorithm, which should be pointed out that gas station refueling stops, refueling along the way so that the least number. For a given n (n <= 5000) and k (k <= 1000) a gas station, the program calculates a minimum number of refueling. Requirements:
Input: the first line has two positive integers n and k, fill up the car can travel represents n km, and the road has k stations. 1 in the next row, there are k + 1 integers, the distance between the k-th and the k-1 stations gas stations. 0 represents the first gas station of departure, fill up the car already. The first k + 1 gas stations expressed destinations.
Output: programming the calculated minimum number of refueling. If you can not reach the destination, the output "No Solution".

Code

#include <the iostream> 

the using namespace STD; 

int main () { 
    int n-, K, I; int * Station; 
    COUT << "Please enter a number to fill the oil tank and the maximum driving distance of the stations:"; 
    CIN n-K >> >>; Station = new new int [K +. 1]; 
    COUT << "Please enter the distance between the adjacent two stations:"; 
    for (I = 0; I <= K; I ++) Station >> CIN [I]; 
    int S = 0, number = 0; // number of times recording refueling 
    s = station [0]; // fill up after a desired travel distance 
	for (i = 1; i < = k; i ++) {// the representative stations number i. . 1 to 7 to be representative of a large refueling station 2 
		if (s> n) {cout << "No solutin !!"; break;} // stations determines whether i reaches 
		else {// i can reach stations 
	  	    s = s + station [i] ; // i + 1 to the next fueling station from a desired exercised 
		   if (s> n) {// desired distance> n- 
 		        Number ++; // refueling 
		        s = station [i]; / / distance to the next gas station 
		        cout <<
	cout<<number<<endl;
	return 0;
   }

 

Guess you like

Origin www.cnblogs.com/khnl/p/11740335.html