【发布时间】:2011-12-05 03:28:00
【问题描述】:
在这种情况下,假设金额为 15,硬币为 1、6、7,那么获得它的方式总数为 6。下面的代码工作正常,但效率不高。任何建议将不胜感激。
public class CoinProblem {
public static void main(String[] args) {
int amount = 15;
int coinTypes[] = {1,6,7};
int combinations = 0;
for (int i = 0; i * 7 <=15; i++) {
for (int j = 0; j * 6 + i * 7 <= 15; j++) {
combinations++;
}
}
System.out.println(combinations);
}
}
【问题讨论】:
-
好吧,我的第一个建议是,如果您要使用数组来存储
coinTypes,那么您...实际上在某处的计算中使用数组... -
给你这个作业的人一定是在重复使用他们的问题,this question 和你的一样,数量为 15,硬币为 1、6、7。它是用 Java 编写的。还有this和this。
-
是的,我想我每个月都会看到一次这个问题。
标签: java