硬币更改算法问题-不起作用

时间:2019-02-18 10:41:25

标签: javascript algorithm coin-change

我正在使用以下算法来解决硬币兑换问题:

for (let coin of coins){
    if(change >= coin){
        if (Math.floor(change/coin) > 0){
            console.log(Math.floor(change/coin)+ " pièces de " + coin + " euro");
            let newChange = change - coin;
            change = newChange;
        }
    }
 }

结果应如下所示:

Purchase of €1.34 paid for with €5:
Change: €3.66 
Coins returned:
2 euro: 1 
1 euro: 1
50 cents: 1 
10 cents: 1 
5 cents: 1 
1 cent: 1

我的结果如下:

Purchase of $1.34 paid for with $5
Change: 3.66
1 pièces de 2 euro
1 pièces de 1 euro
1 pièces de 0.5 euro
1 pièces de 0.1 euro
1 pièces de 0.05 euro
1 pièces de 0.01 euro

1 个答案:

答案 0 :(得分:0)

请注意,您的行:<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> 是错误的,因为它假定只给出了1个硬币,并且忽略了多个硬币的情况。

更改var的新分配应该是减少硬币后更改的模数,因此将您的if更改为:

let newChange = change - coin;