xthexder

Untitled

Feb 25th, 2012
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.52 KB | None | 0 0
  1. public class Coins {
  2.     public static int[] V = new int[] {1, 3, 5};
  3.     public static int S = 11;
  4.    
  5.     public static void main(String[] args) {
  6.         System.out.println(recurse(2, 0));
  7.     }
  8.    
  9.     public static int recurse(int i, int total) {
  10.         int min = -1;
  11.         int num = 0;
  12.         if (i < 0) return (S - total == 0) ? 0 : -1;
  13.         for (int j = (S - total) / V[i]; j >= 0; j--) {
  14.             int tmp = recurse(i - 1, total + V[i] * j);
  15.             if (tmp >= 0 && (min == -1 || tmp < min)) {
  16.                 min = tmp;
  17.                 num = j;
  18.             }
  19.         }
  20.         return min + num;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment