英文:
Store total coin value using lowest amount of coins?
问题
我正在尝试编写一个代码,以反映以下说明。
一个方法,接受两个值;要交换的值和要排除的硬币类型,然后返回所需的最少硬币来交换总值,并将输出返回为字符串。例如,multiCoinCalculator(756, 50) 可能会返回 "需要交换的硬币为:3 x 200p,1 x 100p,0 x 50p,2 x 20p,1 x 10p,剩余 6p"。
我在这里编写的代码返回了需要多少个每种硬币来组成该值。
public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("需要交换的硬币为:");
for (int c : coinList) {
if (c == coinValue) {
System.out.println("0 " + c + "p");
} else {
int result = (coin / c) * c;
System.out.println(result / c + " " + c + "p。");
}
}
}
当我输入硬币值 756 和排除硬币值 50 时,我得到以下结果:
需要交换的硬币为:
3 200p。
7 100p。
15 50p。
37 20p。
75 10p。
是否可能获得有关如何修复这个问题的建议?
英文:
I am trying to write a code that reflects the below instructions.
A method that takes two values; the value to exchange, and the coin type to exclude, and then return the minimum coins needed to exchange the for the total value, and return the output as a String. For example multiCoinCalculator (756,50) may return "the coins to exchange are : 3 x 200p, 1 x 100p, 0x50, 2 x 20p, 1 x 10p, with a remainder of 6p".
The code I have written here returns how much of each coin is needed to make that value.
public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("The exchanged coins are: ");
for (int c : coinList) {
if ( c == coinValue) {
System.out.println("0 " + c + "p");
}
else {
int result = (coin/c)*c;
System.out.println(result/c + " " + c + "p. ");
}
}
When I enter 756 as coin value and 50 as excluded coin I get the following:
The exchanged coins are:
3 200p.
7 100p.
15 50p.
37 20p.
75 10p.
Would it be possible to get any advice on how to fix this?
答案1
得分: 1
public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("The exchanged coins are: ");
for (int c : coinList) {
if (c == coinValue) {
System.out.println("0 " + c + "p");
} else {
int result = (coin / c);
System.out.println(result + " " + c + "p. ");
coin = coin % c;
}
}
System.out.println("Remainder of ," + coin + "p. ");
}
英文:
public void multiCoinCalculator(int coin, int coinValue) {
System.out.println("The exchanged coins are: ");
for (int c : coinList) {
if ( c == coinValue) {
System.out.println("0 " + c + "p");
}
else {
int result = (coin/c);
System.out.println(result + " " + c + "p. ");
coin=coin%c
}
}
System.out.println("Remainder of ,"+ coin +"p. ")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论