Google Answers Logo
View Question
 
Q: Trying to understand an algorithm ( No Answer,   0 Comments )
Question  
Subject: Trying to understand an algorithm
Category: Computers > Algorithms
Asked by: satamas-ga
List Price: $10.00
Posted: 03 Oct 2004 20:40 PDT
Expires: 03 Oct 2004 21:43 PDT
Question ID: 409908
I'm trying to understand the following algorithm. Its function is to
take as input an integer signifying a certain number of cents, and then
any number of integers representing coin denominations from greatest
to least. A 1 cent coin is required. The output is the minimum number
of coins necessary to produce the total number of cents from the
denominations provided. For example,
15 5 1
would output 3, as 5+5+5 is the minimum way to make 15

Using divide and modulus won't work because of cases like 15 6 5 1,
because using 6 cent coins would result in using 5 total.


#include <iostream>
using namespace std;

int min(int a, int b){
  if(a<b)
    return a;
  return b;
}

int main(){
  int amt;
  int den;
  int coins=0;
  int * store;
  int maxguess=0;
  while(cin>>amt>>den){
    store=new int[amt+1];
    for(int n=0;n<amt+1;n++)
      store[n]=n;
    while(den>1){
      for(int j=den; j<=amt;j++){
        store[j]=min(store[j], store[j-den]+1);
      }
      cin>>den;
    } 
    cout<<store[amt]<<'\n'; 
    delete store;
  }
  return 0;
}

Everything except

for(int j=den; j<=amt;j++){
store[j]=min(store[j], store[j-den]+1);
}

is pretty trivial. Any ideas how this thing works?  The store[j-den]+1
has me especially confused.
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy