Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Comp Sci Help [LOOPS]

Determine all possible ways to make change for a dollar using only quarters, dimes, nickels and pennies. The output must show the results, one line at a time,
without showing zeros. Count the number of ways that the program finds to make change and print this result at the end of the program.

For example: 2 quarters, 5 dimes NOT 2 quarters, 5 dimes, 0 nickels, 0 pennies.

Honestly. I'm done with java.
You want a solved solution or a hint?


make a counter for each type
make a balance variable
Try to put the balance divided by the counter size (i.e balance = 90 cents, quarter size is 25, so quarter_Counter = balance / quarter_size)
then remove the size * counter from the balance and keep going down till you reach the smallest value.

Then just do an if statement for each of the counters.
[Image: 5WnumJy.png]
bitcoin:14L3Af8XPZRbmbcfEZTFpCZbHv2D7ujdAy
(Nov 12, 2019 04:57 AM)Varius Wrote:  You want a solved solution or a hint?


make a counter for each type
make a balance variable
Try to put the balance divided by the counter size (i.e balance = 90 cents, quarter size is 25, so quarter_Counter = balance / quarter_size)
then remove the size * counter from the balance and keep going down till you reach the smallest value.

Then just do an if statement for each of the counters.

Could you perhaps explain it using 4 for loops?
I like to help but dont get the question, still not good in English. I dont get quarter, dimes, pennies and nickels.
(Nov 12, 2019 07:42 AM)Accord Wrote:  I dont get quarter, dimes, pennies and nickels.

The question: How many ways can you count to 100 using only 25, 10, 5, 1.

Examples:
  • 1+..+1+1+1+1+1=100
  • 1+..+5=100
  • ...
  • 25+25+25+25=100
(Nov 12, 2019 07:20 AM)Yukino Wrote:  Could you perhaps explain it using 4 for loops?

Using 4 loops:
Code
public static int breakCentsDown(int num) {
    int count = 0;
    for (int q = 0; q <= num / 25; q++) {        //0 to max quarters in num (100 in this case) cents
        for (int d = 0; d <= num / 10; d++) {    //0 to max dimes in num (100 in this case) cents
            for (int n = 0; n <= num / 5; n++) { //0 to max nickels in num (100 in this case) cents
                for (int p = 0; p <= num; p++) { //0 to max pennies in num (100 in this case) cents
                    int v = q * 25 + d * 10 + n * 5 + p; //calculate current total
                    if (v == num) {              //if desired amount, output and increase counter
                        System.out.println(String.format("%d quarter(s), %d dime(s), %d nickel(s), and %d pennies", q, d, n, p));
                        count++;
                    } else if (v > num) {        //if over value, no longer need to waste time computing
                        break;
                    }
                }
            }
        }
    }
    return count;
}
public static void main(String[] args) {
    System.out.println("Total ways to break cents down: " + breakCentsDown(100)); //100 cents = $1, alternatively can pass 1 and multiply num by 100 in the function
}
(Does not adhere to all specifications in the original post)
I post my opinions, please try not to be offended... I am not saying I am superior, but my interpretation of what you are saying may be befuddling.
(Nov 12, 2019 12:17 PM)GenericSkid Wrote:  The question: How many ways can you count to 100 using only 25, 10, 5, 1.

Examples:
  • 1+..+1+1+1+1+1=100
  • 1+..+5=100
  • ...
  • 25+25+25+25=100

Oh am very thankful, I dont really know the value of them and now I get it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by n00b.caApr 4, 2020 12:39 PM 3 201 Apr 4, 2020 12:39 PM
Last Post: n00b.ca
[C] by GoddessTaliyahMay 3, 2018 02:22 PM 4 395 May 3, 2018 02:22 PM
Last Post: 0xDEAD10CC
[Java] by ComputroniumMar 3, 2018 11:38 AM 2 228 Mar 3, 2018 11:38 AM
Last Post: Computronium