# 贪心算法-找零问题

1分，2分，5分，1角，2角，5角，1元面值硬币，怎么找零给顾客硬币数最少

```python
'''
贪心思路：从面值最大的开始找起
'''
d=[0.01,0.02,0.05,0.1,0.2,0.5,1.0]

d_num=input('零钱数量：')
d_num=[int(i) for i in d_num.split(' ')] #每个币种数量

sum_cash=sum([d[i]*d_num[i] for i in range(len(d_num))]) #硬币总面值
print('硬币总面值:',sum_cash)

back_cash=input('找零钱数：')
back_cash=float(back_cash)


if back_cash>sum_cash:
    print('无法找零')
else:
    sum_cash=sum_cash-back_cash
    i=6 #尝试遍历7个币种，从面值最大的开始，
    while i>=0:
        if back_cash>=d[i]:
            n=int(back_cash/d[i]) #需要找i号币数量
            if n>=d_num[i]:#如果没那么多数量，全部取出
                n=d_num[i]
            back_cash-=n*d[i]
            print('%d号币:%f,找回%d个'%(i,d[i],n))
        i-=1
```

```
零钱数量：12 11 11 11 11 11 11
硬币总面值: 20.69
找零钱数：1.8
6号币:1.000000,找回1个
5号币:0.500000,找回1个
4号币:0.200000,找回1个
3号币:0.100000,找回1个
```


---

# 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/zhao-ling-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.
