# 贪心算法-汽车加油问题

汽车加满油可行驶n km，途中k个加油站，计算最少加油次数

```python
'''
贪心思路：每次尽可能开最长距离，再加油，
'''
def greedy_car():
    n=100
    k=5
    d=[50,80,39,60,40,32] #起点->(50km)->1->(80km)->2>(39km)->3>(60km)->4>(40km)->5->(32km)->终点
    num=0
    for i in range(k):
        if d[i]>n:
            print('不满足数据')
            break

    i,s=0,0
    while i<=k:
        s+=d[i]
        if s>=n:
            num+=1
            s=d[i]
        i+=1
    print(num)
greedy_car()
```

```
3
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/si-xiang-lei/tan-xin/qi-che-jia-you-wen-ti.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
