Description
A car can travel n kilometres after filling it with oil. There are a number of gas stations during the trip. An effective algorithm is designed to indicate
At which gas stations to stop refueling, so that the least number of refueling along the way. For a given n (n <= 5000) and K (k <= 1024) gas station locations, the minimum refueling times are calculated programmatically.
Input
The first line has 2 positive integers n and K, which means that the car can travel n kilometers after filling with oil, and there are K gas stations on the road. In the next 1 lines, there are k+1 integers, which in turn represent the distance between the first gas station and the first I-1 gas station. And the 1th integer represents the distance between the first gas station and the starting point, when the car has been filled with oil. The first k+1 integer indicates the distance between the destination and the last gas station.
Output
The minimum number of refueling times calculated by the output programming. If the destination cannot be reached, the "No solution!" is output.
Sample Input
7 7
1 2 3 4 5 1 6 6
Sample Output
4
CODE
Package com.tset;
Import Java.util.Iterator;
Import Java.util.Scanner;
Import Java.util.Vector;
/**
*
* @author DSW
* Car Refueling problem
*
*/
public class Testcaradd {
/**
* Length: The vehicle can travel length kilometer after filling with oil
* NUM: There are num gas stations on the trip.
* Array: Indicates the distance between the first gas station and the I-1 gas station.
* and the 1th integer indicates the distance between the first gas station and the starting point, and the car is filled with oil at the beginning.
* K+1 An integer indicating the distance between the destination and the last gas station.
* @param args
*
*/
public static void Main (string[] args) {
Scanner sc = new Scanner (system.in);
int length = Sc.nextint ();
int num = Sc.nextint ();
vector<integer> array = new vector<integer> ();
for (int i=0;i<=num;i++) {
int k = Sc.nextint ();
Array.add (i, k);
}
Addgas (Array,length,num);
}
/**
* Car Refueling method
* Totlength: Record the Journey Through
* Count: Record refueling times
* @param array
* @param length
* @param num
*/
public static void Addgas (vector<integer> array,int length,int num) {
Iterator<integer> it = Array.iterator ();
int totlength = 0;
int count = 0;
while (It.hasnext ()) {
int next = It.next ();
if (next>length) {
System.out.println ("No solution!");
} else {
Totlength =next+totlength;
if (totlength>length) {
Count+=1;
Totlength = Next;
}
}
}
System.out.println (count);
}
}