Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <string>
- using namespace std;
- int numStations = 0; //# of gas stations
- const int MAXSTATIONS = 100;
- double distances[MAXSTATIONS] = {0}; //first entry is not gas station
- double prices[MAXSTATIONS] = {100}; //it is starting position
- double range; //range of the car, distance it can go with full tank of gas
- //find the gas station with the cheapest gas, within the specified range
- int minGasPriceIndex(int startIndex, int stopIndex) {
- double minPriceSoFar = prices[startIndex];
- double minIndexSoFar = startIndex;
- for(int i=startIndex+1; i<=stopIndex; i++)
- if (prices[i] < minPriceSoFar) {
- minPriceSoFar = prices[i];
- minIndexSoFar = i;
- }
- return minIndexSoFar;
- }
- //find the last gas station before reaching distance.
- int findLastIndex(double distance) {
- int index = numStations;
- while (distances[index] >= distance)
- index--;
- return index;
- }
- //how many gas stops we need to make to reach from distances[currentIndex] to destDistance?
- int gasStops(int currentIndex, double destDistance) {
- }
- //extract the entries from line and put the data into specified array.
- //returns # of entries stored in array
- int extract(string line, double array[]) {
- istringstream istr(line);
- int lastIndex = 1; //index 0 is used by starting position
- while (istr >> array[lastIndex])
- lastIndex++;
- lastIndex--;
- return lastIndex;
- }
- int main() {
- string line;
- double distToDestination;
- getline(cin, line); //distances
- numStations = extract(line, distances);
- getline(cin, line); //prices
- extract(line, prices);
- cin >> range >> distToDestination;
- cout << gasStops(0, distToDestination);
- }
Advertisement
Add Comment
Please, Sign In to add comment