Correct Change Advanced
Optional NodeYou are (again) given three numbers as input: nickels
, pennies
and sum
. Can you select the exact change from your coins to reach a sum
of cents? Return true
if you can, and false
otherwise.
In this challenge, you can be given any number of nickels and pennies.
Guideline: You do not need any loops to solve this challenge. You can build on your code from the previous challenge to solve this one.
Input/Output Format
Boilerplate code is provided for Java. For other languages, read in the numbers from the standard input in the following format.
- The input starts with N, the number of cases.
- N lines follow, which each contains 3 integers in order for
nickels
,pennies
andsum
.
Print out true or false to the output for each case on its own line.
Challenge
Return true if you can get the exact sum or false otherwise.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
as;ldkj
Dec 29, 10:29 PMAll of them work except for Other input which gives me Your Output instead of Other Output.
????
Learneroo
Dec 29, 10:51 PMOther input and Other Output are additional cases that aren't displayed on this page. For example, given 5 nickels and 5 pennies, can you reach a sum of 35? Its not possible, but your code returned
true
.Peter Lacres
Jan 8, 4:36 AMI have the same problem. All examples are correct but other input/output isn't. What's wrong with my code? Cant think of a case in which it wouldn't work. It would work with the 5 nickels, 5 pennies and sum 35 example.
Learneroo
Jan 8, 9:21 AM@peter, what if you were given 100 nickels, 200 pennies but you needed to get 701 cents? What would your program return then and what should it?
Peter Lacres
Jan 10, 4:10 AMThx, because of the example you have given I was able to find what had to be added to the code. Maybe you should add it to the core I/O instead of other I/O.
David
Mar 6, 3:45 PMCool i actually did not have to modify my code from the previous example, the same one worked :)
saeed
Mar 31, 1:35 AMHow you must write a good program:
First:Solve the problem yourself
Second:Change your resolution to code exactly
Third:Simplify your code by getting your necessary final conclusion without doing whole operation
Mendel Simon
Jun 23, 9:18 AMThis one was quite the challenge, but I finally figured it out!
.
.
.
boolean yes=false;
for (int i=0; i<=nickels; i++){
if (5*(nickels-i)<=sum){
Thank you for this! Very rewarding.
shang
Jul 7, 2:26 AMViktor Ayadi
Jul 27, 9:10 AMthales
Jul 16, 8:22 AMmy code
Prateek
May 12, 5:01 AMpublic static boolean doStuff(int nickels, int pennies, int sum){
int a=sum/5;
if(a>nickels){
a=nickels;
}
Jonathan Solorzano
May 31, 11:32 AMWith my code, Im able to get all correct except the last one with 7, 7, 44..why would it not work?
Paul
May 25, 8:50 AMBoiler plate code for ruby:
ruby
n = gets.chomp.to_i
n.times do
input = gets.chomp.split(" ").map(&:to_i)
nickels = input[0]
pennies = input[1]
desired_sum = input[2]
print correct_change?(nickels, pennies, desired_sum), "\n"
end