#include
int change(int amount, int index, int const coins[]) {
if(amount == 0) return 1;
if(index <= 0) return 0;
for(int i = amount / coins[index - 1]; i >= 0; --i) {
if(change(amount - i * coins[index - 1], index - 1, coins)) {
if(i)
printf(" %d*%d", i, coins[index - 1]);
return 1;
}
}
return 0;
}
int main()
{
int coins[] = { 20, 50};
int const size = sizeof(coins)/ sizeof(int);
int amount;
for(int i = 0 ; i
for(int j = i + 1 ; j
if(coins[i] > coins[j]) {
int temporary = coins[i];
coins[i] = coins[j];
coins[j] = temporary;
}
}
}
if(coins[0] <= 0) {
printf("数据有误,零钱必须大于0\n");
return -1;
}
printf("请输入要兑换的货币金额: ");
scanf("%d", &amount);
if(change(amount, size, coins))
printf("\n兑换成功\n");
else printf("\n兑换失败\n");
return 0;
}