[ create a new paste ] login | about

Link: http://codepad.org/ffppgOdK    [ raw code | output | fork ]

C++, pasted on May 24:
// C++ program to find number of ways to get sum 'x' with 'n'
// dice where every dice has 'm' faces
#include <iostream>
#include <string.h>
using namespace std;

// The main function that returns number of ways to get sum 'x'
// with 'n' dice and 'm' throws.
int findWays(int m, int n, int x)
{
    // Base cases
    if (x < 1)   return 0;
    if (n == 1)  return (x <= m);

    int i, numberOfWays = 0;
    for (i = 1; i <= m; ++i)
        numberOfWays += findWays(m, n-1, x - i);
    return numberOfWays;
}

// Driver program to test above functions
int main()
{
    cout << findWays(4, 2, 1) << endl;
    cout << findWays(2, 2, 3) << endl;
    cout << findWays(6, 3, 8) << endl;
    cout << findWays(4, 2, 5) << endl;
    cout << findWays(4, 3, 5) << endl;

    return 0;
}


Output:
1
2
3
4
5
0
2
21
4
6


Create a new paste based on this one


Comments: