Hy家伙,出于某种原因,我的贪婪硬币更改程序不起作用。该函数应返回最小数量的硬币,您可以更改一个值,并且还有一个数组,其中包括可用于此的硬币。我的程序没有显示任何内容,我不知道为什么。硬币更改贪婪算法
public class Main {
public static int coinChangeGreedy(int[] coins, int n) {
int result = 0;
while (n != 0)
{
for (int i=coins.length - 1 ; i>=0 ; i--)
{
if (coins[i] <= n)
{
n = n - coins[i];
result++;
}
}
}
return result;
}
public static void main(String[] args)
{
int[] coins = {1, 2, 5};
int n = 11;
coinChangeGreedy(coins, n);
}
}
贪婪算法不适合这个问题,你应该使用''动态编程''而不是 –
它不显示任何东西,因为你什么都不打印...... –
*以及其中包括你可以使用的硬币* - 你也没有跟踪那 –