Automotive Fuel Filling problem (algorithm design) original version of VC ++

Source: Internet
Author: User

 

Problem description: a car can run n kilometers after it is filled with oil. There are several gas stations on the trip. To minimize the number of fuel stations along the way, design an effective algorithm. For the given N and K gas stations, specify the gas stations at which to stop for fuel to minimize the number of fuel stations. In the input data, the first line has two positive integers, indicating that the car can travel n kilometers after it is filled with oil, and there are K gas stations on the trip. In the next line, there are k + 1 integers, indicating the distance between the K gas station and the K-1 gas station. 0th gas stations indicate departure from the ground, and the car is full of oil. The gas station k + 1 represents the destination. The output is the minimum number of refuelling times. If the destination cannot be reached, the output is "no solution ".

Code:

# Include <iostream. h>

Int add_station (int n, int K, int A [], int A []) // the minimum number of gas stations required to arrive at the destination site

{

Int I, S = 0;

 

For (I = 0; I <= K; I ++)

{If (a [I + 1]> N) {cout <"no solution" <Endl; return 0; break ;}

 

Else

{

S + = A [I];

If (S + A [I + 1] <n) A [I] = 0;

Else {A [I] = 1; S = 0 ;}

}

 

}

 

Return 1;

}

Int main ()

{

Int I, k, n, t, s = 0;

Cout <"Enter the maximum driving distance for a car to refuel:"; CIN> N;

Cout <"Enter the number of gas stations during the trip:"; CIN> K;

Int * A = new int [k + 1 + 1];

Int * A = new int [k + 1 + 1];

A [0] = 0;

Cout <"Enter the distance of each site (separated by spaces)" <k + 1 <"segment:/N ";

For (I = 1; I <= k + 1; I ++)

Cin> A [I];

 

For (I = 0; I <k + 1; I ++)

A [I] = 0;

Cout <"minimum output fuel count:/N ";

T = add_station (n, k, A, );

If (t = 0) return 0;

Else {

 

For (I = 0; I <k + 1; I ++)

S + = A [I];

Cout <S <Endl;

}

}

 

 

 

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.