> Hi,

> I am trying to write a program to count dices. The program takes a

> single integer 'n' (number of dice), it then outputs between 'n' and

> '6*n', what combinations of the n dice result sums to it. Say, n=4, sum=5,

> then it outputs 1 1 1 2 5, 1 1 2 1 5, 1 2 1 1 5, 2 1 1 1 5. I am stuck

> with a problem, since my program is supposed to accept arbitrary number of

> dice, I can't come up with a way to code the loops. Remember it needs to

> iterate through arbitrary number of dices. Here is what I have

> (compilies/runs/result is wrong for obvious reasons)

>

> Let me know if you can come up with a smart pattern to solve this:
If n is small, you can use a single unsigned integer to store the

combinations. A 64 bit integer can work with n <=18, this will be a lot

faster than using a vector.

If that is the case, the solution is trivial:

int n = 4;

int sum = 8;

int start = sum - n + std:

ow(10.0, n)/9;

int end = start/10 + std:

ow(10.0, n - 1) * (sum - n + 1);

while (start <= end)

{

std::cout << start << "\n";

start += 9;

}

Of course, this assumes a 10-sided dice

. How to adjust the algorithm so

that it outputs 6-sided dice numbers is left for you as an exercise.

Regards,

Ben