-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedyAlgo.java
More file actions
53 lines (40 loc) · 1.08 KB
/
GreedyAlgo.java
File metadata and controls
53 lines (40 loc) · 1.08 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
class GreedyAlgo {
public static void main (String args[]) {
int[] list = new int[args.length];
for (int i = 0; i < args.length; i++){
list[i]= Integer.parseInt(args[i]);
}
System.out.println("Original amount: "+ list[0] +" cents");
greedSort(list);
}
public static void greedSort(int[] list){
int coin1= 0;
int coin2= 0;
int coin3= 0;
int coin4= 0;
while (list[0] > 0 ){
if (list[0]>=25) {
System.out.println("Total number of Quarters: " + list[0]/25);
coin1 = list[0]/25;
list[0]-=list[0]/25*25;
}
else if (list[0]>=10) {
System.out.println("Total number of Dimes: " + list[0]/10);
coin2 = list[0]/10;
list[0]-=list[0]/10*10;
}
else if (list[0]>=5) {
System.out.println("Total number of Nickels: " + list[0]/5);
coin3 = list[0]/5;
list[0]-=list[0]/5*5;
}
else if (list[0]>=1) {
System.out.println("Total number of Pennies: " + list[0]/1);
coin4 = list[0]/1;
list[0]-=list[0]/1*1;
}
}
int total = coin1+coin2+coin3+coin4;
System.out.println("Total number of Coins: "+total);
}
}