假定有 10元、5 元、 1元、 5角、 1角共 5 种面值零钱,在给顾客找零钱时,一般都会尽可能地选用零钱个数最少的方法。例如,当要给某顾客找 46 元 5 角时,会给他四个10元, 1 个 5元,1个1元和 1 个 5角的零钱。试编写一个程序,输入的是要找给顾客的零钱(以角为单位,例如46元5角应输入465),输出的是应该找回的各种面值零钱的数目,并保证找回的零钱总个数最少。
时间: 2023-03-27 14:03:20 浏览: 135
好的,我可以回答这个问题。以下是程序的实现:
#include <stdio.h>
int main()
{
int money, count10 = , count5 = , count1 = , count05 = , count01 = ;
printf("请输入要找给顾客的零钱(以角为单位):");
scanf("%d", &money);
count10 = money / 10;
money %= 10;
count5 = money / 5;
money %= 5;
count1 = money;
count05 = money * 2;
money = ;
count01 = money * 10;
money = ;
printf("找零的面值和数量分别为:\n");
printf("10元:%d\n", count10);
printf("5元:%d\n", count5);
printf("1元:%d\n", count1);
printf("5角:%d\n", count05);
printf("1角:%d\n", count01);
return ;
}
输入要找给顾客的零钱后,程序会自动计算出需要找回的各种面值零钱的数量,并输出结果。例如,输入465后,程序会输出:
找零的面值和数量分别为:
10元:4
5元:1
1元:1
5角:
1角:
这样就保证了找回的零钱总个数最少。
相关问题
假设商店货品价格都不大于100元(且为整数),若顾客付款在100元内,现有一个程序能在每位顾客付款后给出找零钱的最佳组合(找给顾客货币张数最少)。 假定此商店的货币面值只包括:50元、20元、 10元、 5元、2元、1元 六种。 编写完程序
以下是一个 Python 的实现:
```python
def change(amount):
denominations = [50, 20, 10, 5, 2, 1]
result = []
for d in denominations:
while amount >= d:
result.append(d)
amount -= d
return result
amount = int(input("请输入付款金额:"))
change_list = change(100 - amount)
print("找零钱的最佳组合为:", change_list)
print("需要找零钱的张数为:", len(change_list))
```
这个程序中,我们定义了一个 `change` 函数,它接受一个付款金额 `amount`,并返回找零钱的最佳组合。在函数中,我们先定义了货币面值列表 `denominations`,然后从大到小枚举每种面值,尽可能地多找当前面值的钱,直到找完为止。
在主程序中,我们首先读入顾客的付款金额,然后调用 `change` 函数计算出找零钱的最佳组合,并输出结果。注意,我们这里计算的是找零钱的张数,而不是面值总额。
html设计找零钱最佳组合假设商店货品价格(R)都不大于100元(且为整数),若顾客付款(P)在100元内,现有一个程序能在每位顾客付款后给出找零钱的最佳组合(找给顾客货币张数最少)。假定此商店的货币面值只包括:50元(N50)、10元(N10)、5元(N5)、1元(N1)四种
面额。以下是一个简单的HTML设计,可以让顾客输入商品价格和付款金额,然后显示最佳找零钱组合:
<!DOCTYPE html>
<html>
<head>
<title>找零钱计算器</title>
</head>
<body>
<h1>找零钱计算器</h1>
<form>
<label for="price">商品价格:</label>
<input type="number" id="price" name="price"><br><br>
<label for="payment">付款金额:</label>
<input type="number" id="payment" name="payment"><br><br>
<button type="button" onclick="calculate()">计算找零钱</button>
</form>
<br>
<p id="result"></p>
<script>
function calculate() {
var price = parseInt(document.getElementById("price").value);
var payment = parseInt(document.getElementById("payment").value);
var change = payment - price;
var n50 = Math.floor(change / 50);
var n10 = Math.floor((change - n50 * 50) / 10);
var n5 = Math.floor((change - n50 * 50 - n10 * 10) / 5);
var n1 = change - n50 * 50 - n10 * 10 - n5 * 5;
var result = "找零钱组合为:";
if (n50 > 0) {
result += n50 + "张50元 ";
}
if (n10 > 0) {
result += n10 + "张10元 ";
}
if (n5 > 0) {
result += n5 + "张5元 ";
}
if (n1 > 0) {
result += n1 + "张1元";
}
document.getElementById("result").innerHTML = result;
}
</script>
</body>
</html>
注意,这个HTML设计使用了JavaScript来实现计算找零钱的功能。在用户点击“计算找零钱”按钮之后,JavaScript会读取用户输入的商品价格和付款金额,然后计算最佳找零钱组合,并将结果显示在页面上。由于这个HTML设计比较简单,可能需要根据实际情况进行修改和优化。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)