import SimpleOutput;

/**
 * A very simple (and inefficient) computation of the minimum
 * number of stamps needed to total a particular price.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of March 2000
 */
public class AltStamps 
{
  // +---------+-------------------------------------------------
  // | Helpers |
  // +---------+

  /**
   * Compute the minimum number of stamps that exactly
   * totals val cents.  Puts the values of the stamps in
   * stampsToBuy, starting at index stampNum.
   * Pre: (1) val >= 0
   *      (2) All stamps values are positive.
   *      (3) It is possible to combine stamps for any value.
   *      (4) It requires no more than MAXSTAMPS stamps.
   * Post: (1) Returns the smallest set of stamps to make value.
   *       (2) Returns null if no stamps are necessary.
   */
  public int[] minimumStamps(int val, int[] stampValues)
  {
    // Base case: You need no stamps for 0 cents.
    if (val == 0) {
      return null;
    }
    // Recursive case: Minimize alternatives
    else {
      int i = 0; // An index into stampValues
      int[] guess; // Our best guess so far as to the minimum.
      int[] nextGuess;  // Another guess to try
      int buyThisTime; // The stamp to buy in this round.

      // Find the first stamp that's still worth buying.
      while (stampValues[i] > val)
        i++;
  
      // We might buy that stamp.
      buyThisTime = stampValues[i];
      guess = minimumStamps(val-stampValues[i], stampValues);
  
      // But we might also buy other stamps.
      for (i = i+1; i < stampValues.length; i++) {
        if (stampValues[i] <= val) {
          nextGuess =
            minimumStamps(val-stampValues[i], stampValues);
          if ( ((nextGuess == null) && (guess != null)) 
               || (nextGuess.length < guess.length) ) {
            buyThisTime = stampValues[i];
            guess = nextGuess;
          } // if it's a better guess
        } // if the stamp is worth buying.
      } // for
  
      // Add the current stamp.
      int[] result;
      if (guess == null) {
        result = new int[1];
        result[0] = buyThisTime;
      }
      else {
        result = new int[1+guess.length];
        for (int j = 0; j < guess.length; j++)
          result[j] = guess[j];
        result[guess.length] = buyThisTime;
      }
      // That's it, we're done
      return result;
    } // recursive case
  } // minimumStamps(int, int[])

  // +------+----------------------------------------------------
  // | Main |
  // +------+

  public static void main(String[] args) {
    int val = 0;
    SimpleOutput out = new SimpleOutput();   
    AltStamps helper = new AltStamps();
    // Set up the array of stamp values.
    int[] stamps = { 1, 5, 20, 33, 50 };

    // Sanity check.
    if (args.length != 1) {
      out.println("Usage: java Stamps value");
      System.exit(1);
    }
    // Get the value.
    try { val = (new Integer(args[0])).intValue(); }
    catch (NumberFormatException e) {
      out.println("The value must be an integer.");
      System.exit(2);
    } // catch
    // Verify that the preconditions are met.
    if (val < 0) {
      out.println("The value must be positive.");
      System.exit(3);
    } // if (val < 0)
    // Compute away!
    int[] result = helper.minimumStamps(val, stamps);
    if (result.length == 0)
      out.println("You need no stamps to make no cents.");
    else if (result.length == 1)
      out.println("You need one stamp to make " + val + " cents.");
    else
      out.println("You need " + result.length + " stamps to make " 
                  + val + " cents.");
    for (int i = 0; i < result.length; i++) {
      out.println("  " + (i+1) + ": " + result[i] + " cents");
    } // for
  } // main(String[])

} // class AltStamps
