0

I am having some trouble with one of my sample input values, everything else here is working correctly besides the last input.

The problem is to find how many different combinations of $2,$5, and $10 bills can make up the value, here are the sample input and output numbers.

Also in this problem, (2+5) and (5+2) are being treated as unique combinations.

1 - 0
2 - 1
7 - 2
50 - 52524

I am getting the correct answer for everything else, but 52271 for 50, and can't seem to figure out why. Here is my code.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

long long permutate(long long bill);
long long calculatePossibleOrders(long long A, long long B, long long C);
long long factor(long long x);

int main()
{
    long long num_cases, i;
    /* code for grabbing c - number of cases to solve */
    scanf("%I64d", &num_cases);

    /* for loop for each case */
    for(i = 0; i < num_cases; i++)
    {
        long long bill = 0;
        scanf("%I64d", &bill);
        printf("%I64d\n", permutate(bill));
    }
} 

long long permutate(long long bill)
{
    long long originalbill = bill;
    long long CurrentBill = originalbill; 
    long long numberofOptions = 0;
    long long A, B, C;


    if(bill == 1 || bill == 3)
    {
        return 0;
    }

    for(A = 0; A <= originalbill; A+=10)
    {
        B = 0, C = 0;
        if((CurrentBill - A) == 0) 
        {
            numberofOptions += calculatePossibleOrders(A, B, C); 
            break;
        }
        for(B = 0; (A + B) <= CurrentBill; B+=5)
        {
            C = 0;
            if((CurrentBill - (A + B)) == 0) 
            {
                numberofOptions += calculatePossibleOrders(A, B, C); 
                break;
            }
            for(C = 0; (A+B+C) <= CurrentBill; C+=2)
            {
                if((CurrentBill - (A + B + C)) == 0) 
                {   
                    numberofOptions += calculatePossibleOrders(A, B, C); 
                    break;
                }
            }
        }
    }

    return(numberofOptions);
}

long long calculatePossibleOrders(long long A, long long B, long long C)
{
    long long combos = 0;
    long long x = 0;

    if(A > 0)
    {
        A /= 10;
    }
    if(B > 0)
    {
        B /= 5;
    }
    if(C > 0)
    {
        C /= 2;
    }


    x = A+B+C;
    combos = factor(x)/(factor(A)*factor(B)*factor(C));

    return(combos);
}

long long factor(long long x)
{
    long long i;
    long long value = 1;
    for(i = 1; i <= x; i++)
    {
        value = value*i;
    }
    return(value);
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
kidchicken
  • 11
  • 3
  • 6
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Mar 30 '16 at 15:56
  • Im not sure this problem Im having here can be solved by a debugger, but ill give it a shot if you think it will help. – kidchicken Mar 30 '16 at 16:02
  • 3
    @kidchicken with the debugger you can stop and analyse every variable and its value. So Paul's statement was, that if you use a debugger you would be able to figure out the problem yourself – Hugo Mar 30 '16 at 16:06
  • 1
    Also, just a comment, but perhaps that would improve readability and help you to debug. Try to remove all these awful `long long`s and switch to `int`s. – ForceBru Mar 30 '16 at 16:08
  • 2
    'Im not sure this problem Im having here can be solved by a debugger,' - why not? You have a repeatable problem with a particular, known input, EXACTLY the kind of bug that can be traced with your debugger as you step through and note all the variable values as you go. – Martin James Mar 30 '16 at 16:27
  • 1
    When ia debugger less than useful? When you have an intermittent problem, perhaps on a distributed, networked system, that only shows up occasionally. Then you need logging, and lots of it. – Martin James Mar 30 '16 at 16:29
  • 2
    Are you aware that 20! is the largest factorial that can be stored in a 64-bit integer? – Weather Vane Mar 30 '16 at 17:02
  • when compiling, always enable all the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use `-Wconversion -std=gnu99` ) – user3629249 Mar 30 '16 at 17:07

0 Answers0