Advertisement
Kwwiker

ATM

Jan 27th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1.  
  2. public class ClassicATM {
  3.     final public static int INF = 1000000000;
  4.     public static void main(String[] args) {
  5.         int s = 25;
  6.         int a [] = {10, 8, 6, 1};
  7.         int d[] = new int[s+1];
  8.         d[0]=0;
  9.         for (int m=1; m<=s; m++) {
  10.             d[m]=INF;
  11.             for (int i=0; i<a.length; i++) {
  12.                 if (m>=a[i] && d[m-a[i]]+1<d[m]) {
  13.                     d[m] = d[m-a[i]]+1;
  14.                 }
  15.             }
  16.         }
  17.         if (d[s]==INF) {
  18.             System.out.println("ERROR");
  19.         } else {
  20.             while(s>0) {
  21.                 for (int i=0; i<a.length; i++) {
  22.                     if (d[s-a[i]]==d[s]-1) {
  23.                         System.out.println(a[i]+" ");
  24.                         s-=a[i];
  25.                         break;
  26.                     }
  27.                 }
  28.             }
  29.         }
  30.     }
  31.  
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement