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;
}
}