-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakingChange.java
More file actions
67 lines (48 loc) · 1.88 KB
/
MakingChange.java
File metadata and controls
67 lines (48 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Author: Norhan Abbas
* Date: 31st of May, 2019
*
* Objective: getting the minimum number of coins
* of a GIVEN denomination
* that adds up to a GIVEN amount of money
*
* Using greedy algorithm and JUnit tests
*/
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.lang.IllegalArgumentException;
import static org.junit.Assert.assertEquals;
public class MakingChange {
// amount for which to make change
public static int[] greedy(int amount) {
int amount_Initial = amount;
// the commonly used coins in US currency -DESCENDINGLY
// You can change them with coins used in another country's currency -DESCENDINGLY
int[] possibleCoins = new int[]{25, 10, 5, 1};
// create an array list
// as we do not know how many coins would be there yet
//RECALL: array lists can be expanded, but arrays CANNOT
ArrayList<Integer> coinsList = new ArrayList<>();
// make sure that the amount entered is a positive one, and not zero
for (int i = 0; i < possibleCoins.length; i++) {
while (amount >= possibleCoins[i]) {
coinsList.add(possibleCoins[i]);
amount -= possibleCoins[i];
}
}
// convert the array list into array
int[] coins = new int[coinsList.size()];
for (int k = 0; k < coinsList.size(); k++) {
coins[k] = coinsList.get(k);
}
// you can uncomment the next 3 lines if you wanna check
// whether my function gets the right combination of coins
/* System.out.println("amount: " + amount_Initial + "¢");
System.out.println("Number of coins: " + coins.length);
System.out.println(Arrays.toString(coins) + "\n");*/
return coins;
}
}