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.
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 12:17 PM
(This post was last modified: Nov 12, 2019 12:18 PM by GenericSkid.)
(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 04:28 PM
(This post was last modified: Nov 12, 2019 04:30 PM by Eternal Darkness.)
(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.
|