-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeAlgo
47 lines (36 loc) · 979 Bytes
/
ChangeAlgo
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
import java.util.ArrayList;
public class ChangeAlgo {
public static void main(String[] args) {
double deno[]= {100,50,20,10,5,2,1};
float change = 66;
ArrayList<Double> c = new ArrayList<Double>();
c.clear();
for(int i=0;i<deno.length;i++)
{
float quo = (float) ((float) change/deno[i]);
float rem = (float) ((float) change%deno[i]);
if (quo>=1)
{
float num = (float) ((float) (change-rem)/deno[i]);
for(int u=0;u<num;u++){
//System.out.println("Number of "+deno[i]);
c.add(deno[i]);
}
//System.out.printf("Number of "+deno[i]+"s: "+num+"\n");
change = (float) (change-(num*deno[i]));
}
}
c.set(c.size()-1, FindImediateGreater(c.get(c.size()-1),deno));
for(int i=0;i<c.size();i++){
System.out.println(c.get(i)+"");
}
}
static double FindImediateGreater(double amount,double[] arr){
for(int i=arr.length-1;i>=0;i--){
if(arr[i]>amount){
return arr[i];
}
}
return 0;
}
}