所有关于网络上变革性问题的讨论都只涉及到理想情况,即我们拥有各种类型无限量的硬币/纸币。
我想处理的是ATM仅有有限数量的10、20、50、100、200美元的纸币,并且必须找到解决方案来找零的情况。
我已经做了类似的事情,但我无法处理110美元的需求。整个算法在withdrawCash()
方法中 - 它可以编译并运行。
110美元的输出:
10 * 1 = 10
20 * 4 = 80
Notes of 10 left are 0
Notes of 20 left are 0
Notes of 50 left are 2
Notes of 100 left are 2
Notes of 200 left are 10
代码:
public class ATM {
/** The Constant Currency Denominations. */
protected static final int[] currDenom = { 10, 20, 50, 100, 200 };
/** The Number of Currencies of each type */
protected static int[] currNo = { 1, 4, 2, 2, 10 };
/** The count. */
protected int[] count = { 0, 0, 0, 0, 0 };
protected static int totalCorpus;
static {
calcTotalCorpus();
}
public static void calcTotalCorpus() {
for (int i = 0; i < currDenom.length; i++) {
totalCorpus = totalCorpus + currDenom[i] * currNo[i];
}
}
public ATM() {
}
public synchronized void withdrawCash(int amount) {
if (amount <= totalCorpus) {
for (int i = 0; i < currDenom.length; i++) {
if (currDenom[i] <= amount) {//If the amount is less than the currDenom[i] then that particular denomination cannot be dispensed
int noteCount = amount / currDenom[i];
if (currNo[i] > 0) {//To check whether the ATM Vault is left with the currency denomination under iteration
//If the Note Count is greater than the number of notes in ATM vault for that particular denomination then utilize all of them
count[i] = noteCount >= currNo[i] ? currNo[i] : noteCount;
currNo[i] = noteCount >= currNo[i] ? 0 : currNo[i] - noteCount;
//Deduct the total corpus left in the ATM Vault with the cash being dispensed in this iteration
totalCorpus = totalCorpus - (count[i] * currDenom[i]);
//Calculate the amount that need to be addressed in the next iterations
amount = amount - (count[i] * currDenom[i]);
}
}
}
displayNotes();
displayLeftNotes();
} else {
System.out.println("Unable to dispense cash at this moment for this big amount");
}
}
private void displayNotes() {
for (int i = 0; i < count.length; i++) {
if (count[i] != 0) {
System.out.println(currDenom[i] + " * " + count[i] + " = " + (currDenom[i] * count[i]));
}
}
}
private void displayLeftNotes() {
for (int i = 0; i < currDenom.length; i++) {
System.out.println("Notes of " + currDenom[i] + " left are " + currNo[i]);
}
}
public static void main(String[] args) {
new ATM().withdrawCash(110);
}
}