Guest User

Untitled

a guest
Mar 22nd, 2021
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int numStations = 0; //# of gas stations
  8. const int MAXSTATIONS = 100;
  9. double distances[MAXSTATIONS] = {0}; //first entry is not gas station
  10. double prices[MAXSTATIONS] = {100}; //it is starting position
  11. double range; //range of the car, distance it can go with full tank of gas
  12.  
  13. //find the gas station with the cheapest gas, within the specified range
  14. int minGasPriceIndex(int startIndex, int stopIndex) {
  15. double minPriceSoFar = prices[startIndex];
  16. double minIndexSoFar = startIndex;
  17. for(int i=startIndex+1; i<=stopIndex; i++)
  18. if (prices[i] < minPriceSoFar) {
  19. minPriceSoFar = prices[i];
  20. minIndexSoFar = i;
  21. }
  22. return minIndexSoFar;
  23. }
  24.  
  25. //find the last gas station before reaching distance.
  26. int findLastIndex(double distance) {
  27. int index = numStations;
  28. while (distances[index] >= distance)
  29. index--;
  30. return index;
  31. }
  32.  
  33. //how many gas stops we need to make to reach from distances[currentIndex] to destDistance?
  34. int gasStops(int currentIndex, double destDistance) {
  35.  
  36.  
  37.  
  38. }
  39.  
  40. //extract the entries from line and put the data into specified array.
  41. //returns # of entries stored in array
  42. int extract(string line, double array[]) {
  43. istringstream istr(line);
  44. int lastIndex = 1; //index 0 is used by starting position
  45. while (istr >> array[lastIndex])
  46. lastIndex++;
  47. lastIndex--;
  48. return lastIndex;
  49. }
  50.  
  51. int main() {
  52. string line;
  53. double distToDestination;
  54.  
  55. getline(cin, line); //distances
  56. numStations = extract(line, distances);
  57. getline(cin, line); //prices
  58. extract(line, prices);
  59.  
  60. cin >> range >> distToDestination;
  61.  
  62. cout << gasStops(0, distToDestination);
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment