我正在尝试编写代码以找到最小数量的硬币进行更改。这是我的代码:
public class MinCoinsToChange {
//private static int q=Integer.MAX_VALUE;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(getmin(new int [] {1,2,3},5));
}
static int getmin(int [] C,int P) {
int q = Integer.MAX_VALUE;
if(P <= 0)
{
return q;
}
for(int i = 0; i < C.length; i++)
q = Math.min(q, getmin(C, P - C[i]));
return q;
}
}
但代码无效。我犯的错误在哪里?
答案 0 :(得分:1)
我可以纠正错误。这是更正
int q=Integer.MAX_VALUE;
if(P<=0)
{
q=0;
return q;
}
for(int i=0;i<C.length;i++)
q= Math.min(q, 1+getmin(C, P-C[i]));
return q;
}