C - Remove Duplicate Results in Recursive Function

后端 未结 1 1755
星月不相逢
星月不相逢 2021-01-14 14:01

I have to create a recursive function that tells you the number of ways a number of cents can be made into change. (Using quarters, dimes nickels, and pennies).

So f

相关标签:
1条回答
  • 2021-01-14 14:19
    #include <stdio.h>
    
    int coins(int, int);
    
    int main(void){
        int num;
    
        printf("Enter an amount of change in cents: ");
        scanf("%d", &num);
    
        printf("There are %d ways to make change for %d cents.\n", coins(num, 0), num);
        return 0;
    }
    
    int coins(int amt, int kind){
        static int kinds[4] = {25, 10, 5, 1};
        int ways=0, i, n;
    
        if(kinds[kind] == 1)//always divisible
            return 1;
    
        n = amt / kinds[kind];
        for(i = 0; i <= n; ++i)
            ways+=coins(amt-kinds[kind]*i, kind + 1);
    
        return ways;
    }
    
    0 讨论(0)
提交回复
热议问题