DP-1424: Coin Change

硬币找零算法

 


StatusIn/OutTIME LimitMEMORY LimitSubmit TimesSolved UsersJUDGE TYPE
stdin/stdout3s8192K29895Standard

Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

 


For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, two 5-cent coins and one 1-cent coin, one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

 


Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 7489 cents.

Input

The input file contains any number of lines, each one consisting of a number for the amount of money in cents.

Output

For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.

Sample Input

 

11
26

Sample Output

4
13

 

 

 


This problem is used for contest: 81 


Submit / Problem List / Status / Discuss

#include<stdio.h>
int main()
{
    int coin[5]={1,5,10,25,50};
    int a[7490]={0};
    a[0]=1;
    for(int i=0;i<5;i++)
      for(int j=coin[i];j<=7489;j++)
         a[j]+=a[j-coin[i]];
    int n;    
    while(scanf("%d",&n)!=EOF)
       printf("%d/n",a[n]);
    return 0;
}  

### 错误分析 在 `coinChange` 函数中,出现 `TypeError: '<=' not supported between instances of 'str' and 'int'` 的错误通常是因为输入的硬币面值或目标金额未正确转换为整数。例如,如果从用户输入获取的数据包含字符串类型而未进行类型转换,则可能导致该错误 [^1]。 ### 解决方法 要解决此问题,必须确保所有输入数据(包括硬币面值和目标金额)都被正确地转换为整数类型。以下是修正后的代码示例: ```python def coinChange(coins, amount): # 初始化dp数组,将所有元素的值设置为无穷大 dp = [float("inf")] * (amount + 1) dp[0] = 0 # 凑成金额0不需要任何硬币 for i in range(1, amount + 1): for coin in coins: if coin <= i and dp[i - coin] + 1 < dp[i]: dp[i] = dp[i - coin] + 1 return dp[amount] if dp[amount] != float("inf") else -1 # 用户输入处理 n = int(input("请输入硬币的种类数量: ")) coins_input = input("请输入每种硬币的面值(用空格分隔): ").split() coins = list(map(int, coins_input)) # 确保硬币面值被转换为整数列表 amount = int(input("请输入需要找零的金额: ")) # 确保目标金额被转换为整数 result = coinChange(coins, amount) print("凑成金额{}所需的最少硬币数量为{}".format(amount, result)) ``` 上述代码通过使用 `map(int, coins_input)` 将输入的字符串转换为整数列表,从而避免了比较字符串与整数时可能发生的错误 [^1]。 ### 避免常见陷阱 - **变量命名冲突**:避免使用 Python 内置函数名作为变量名,如 `str`、`list` 等,否则会导致类似 `TypeError: 'str' object is not callable` 的错误 [^2]。 - **全局变量污染**:如果之前执行的代码中定义了与内置函数同名的变量(如 `str`),即使当前代码中没有定义这些变量,也可能导致错误。此时建议重启编辑器以清除全局变量 [^2]。 - **数据类型一致性**:在涉及数值运算的场景中,确保所有数据均为数值类型(如 `int` 或 `float`)。可以通过 `astype(float)` 或 `astype(int)` 强制转换 Pandas 数据框中的列类型 [^3]。 ### 总结 通过确保所有输入数据被正确转换为整数类型,可以有效避免 `TypeError: '<=' not supported between instances of 'str' and 'int'` 错误。此外,还需注意变量命名冲突和全局变量污染的问题,以确保程序的稳定运行。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值