Thread: Need Help

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Need Help

    I need help starting this program I have to Write a program that will determine the maximum number of combinations to make $1 in change given six different coins. The coins you can use are pennies, nickels, dimes, quarters, fifty cent pieces, and the Susan B. Anthony dollar. For example, three possible combinations are:

    Number of Coins Type
    1 - Susan B. Anthony dollar
    2 - 2 fifty cent pieces
    3 - 2 quarters, 1 fifty cent piece

    thanks for the help

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    i used 6 nested loops and the line

    if(((a * SBA) + (b * FCP) + (c * Q) + (d * D) + (e *N) + (f * P)) == 100)
    then solution found

    where
    a - f represent the # of a given coin and are loop variables
    SBA = 100;
    FCP = 50;
    Q = 25;
    D = 10;
    N = 5;
    P = 1;

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Try this, its not particularly efficient but it appears to work:

    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int main() {
        int numCombos = 0;
    
        cout << "Dollars HDollars Quarters Dimes Nickels Pennies" << endl;
        cout << "------- -------- -------- ----- ------- -------" << endl;
    
        for (int dollars = 0; dollars <= 1; dollars++) {
          for (int halfdollars = 0; halfdollars <= 2; halfdollars++) {
            for (int quarters = 0; quarters <=4; quarters++) {
              for (int dimes = 0; dimes <= 10; dimes++) {
                for (int nickels = 0; nickels <= 20; nickels++) {
                  for (int pennies = 0; pennies <= 100; pennies++) {
                    if ((dollars*100 + halfdollars*50 + quarters*25 + dimes*10 + nickels*5 + pennies) == 100) {
                      numCombos++;
                      printf("%7d %8d %8d %5d %7d %7d\n", dollars, halfdollars, quarters, dimes, nickels, pennies);
                    }
                  }
                }
              }
            }
          }
        }
    
        cout << endl << "Total Combos:  " << numCombos << endl;
    }
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    thanks for the fast reply

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Wow, I've never seen that many nested loops!!
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    82
    Yeah, every fiber in my efficiency-loving being rebelled against doing it that way, but I couldn't think of anything better
    Claus Hetzer
    Compiler: Borland 5.5 (on Windows)
    Solaris CC (on Unix)
    Known Languages: C++, MATLAB, Perl, Java

Popular pages Recent additions subscribe to a feed