Python Little Exercise

Source: Internet
Author: User

There are n petrol stations on a circular road, numbered 0,1,2,... n-1, each filling station has an upper limit, which is kept in the list limit, that is, Limit[i] is the upper limit for refueling at the petrol filling station, while driving from the I petrol station to (i+1)%n a petrol station requires cost[ I] liter oil, cost is a list. Now there is a car with no oil at the beginning, and to go around the road from a gas station and back to the beginning. give you the integer n, the list limit and the list cost, and you decide if you can complete the task. If the task can be completed, output the starting gas station number, if there are multiple, the output number is minimal. If the task cannot be completed, output-1.

Gas=0
Res=[]
For I in range (n):
Gas=0
For j in Range (N):
gas+=limit[(i+j)%n] % start with while to do more trouble
gas-=cost[(I+J)%n]
If Gas<0:break
If gas>=0:
Res.append (i)
If Len (res) ==0:print-1 % appends all possible numbers into the list, if Len = = 0, it represents no possible result
else:p rint (Res[0])

Python Little Exercise

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.