Correct Change Advanced

Optional Node
Collapse Content

You 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 and sum.

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

  • All of them work except for Other input which gives me Your Output instead of Other Output.
    ????

  • Other 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.

  • I 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.

  • @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?

  • Thx, 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.

  • Cool i actually did not have to modify my code from the previous example, the same one worked :)

  • How 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

  • This 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){

    cont...
  •     if(sum%5 > pennies) {
            return false;
        }
        return nickels*5 + pennies >= sum;
    
  • public static boolean doStuff(int nickels, int pennies, int sum){
        for (int i = 0; i <= nickels; i = i + 1) {
            if ( sum - 5 * i <= pennies && sum - 5 * i >= 0) {
                return true;
            }
        }
    
        return false;
    }
    
  • public static boolean doStuff(int nickels, int pennies, int sum){
    int a=sum/5;
    if(a>nickels){
    a=nickels;
    }

        int b=sum-(a*5);
        if(pennies>=b){
            return true;
        }
        return false;
    }
    
  • With my code, Im able to get all correct except the last one with 7, 7, 44..why would it not work?

  • Boiler 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

Contact Us
Sign in or email us at [email protected]