Norway


#include <stdio.h>

 

#define N 10

#define 100

 

// create a lookup table to store solutions of subproblems

// lookup[i][j] stores number of to sum j

// with j of given .

int lookup[N][SUM];

 

// Function to calculate number of ways to achieve given

// sum with n throws of dice having k

int count(int n, int k, int sum)

{

    // if desired sum is reached with n dices

    if (n == 0)

        return (sum == 0);

 

    // desired sum can17;t be reached with current configuration

    if (sum < 0 || k * n < sum || n > sum)

        return 0;

 

    // if sub-problem is seen for the first , solve it and

    // store its result in an array or map

    if (lookup[n][sum] == 0)

    {

        // recuse for all possible solutions

        for (int i = 1; i <= k; i++)

            lookup[n][sum] += count(n 1, k, sum i);

    }

    

    // return solution to current sub-problem

    return lookup[n][sum];

}

 

// main function

int main(void)

{

    int n = 4;    // n throws

    int k = 6;    // values 1 – 6

 

    int sum = 15;    // desired sum

 

    printf(“Total number of ways are %d”, count(n, k, sum));

 

    return 0;

}



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here