fork(10) download
  1. #include <iostream>
  2. #include <stack>
  3. using namespace std;
  4.  
  5. //float fuel[] = { 5,3,12,1,7 }; float dist[] = { 8,4,7,4,5 };
  6. float fuel[] = { 3,4,6,3,7,11 }; float dist[] = { 3,10,2,4,6,9 };
  7. int n = 6;
  8. int FindMinimumPosition()
  9. { float last = 0; int position = 0; float minimumFuel = 1; int minimumPosition = 0;
  10. while (position < n)
  11. { last += fuel[position];
  12. last -= dist[position];
  13. position++; // could be n which is past array bounds
  14. if (last < minimumFuel)
  15. { minimumFuel = last; minimumPosition = position % n; //in case of last station
  16. }
  17. }
  18. return minimumPosition ;
  19. }
  20. int main(){
  21. cout<< FindMinimumPosition();
  22. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
2