How can I use C++ to solve the following problem?

List how many ways the numbers 9, 13, 17, 25, 31, and 35 can be added exactly 6 times to equal 100.

Printable View

- 09-15-2002KaoruC++ problem w/ sums
How can I use C++ to solve the following problem?

List how many ways the numbers 9, 13, 17, 25, 31, and 35 can be added exactly 6 times to equal 100. - 09-15-2002The Dog
Explain the problem more clearly.

Can the numbers be used more than once? - 09-15-2002Kaoru
Yes, they can be. I thought about using an array and something with recursion, but I just can't figure it out. :o

- 09-15-2002Mister C
Attempt to write some code and then post it. It is important you understand the problem first!!

Remember to use the code tags!!

Mr. C. - 09-15-2002Stoned_Coder
Think nested loops, an array and an if statement!

- 09-16-2002Kaoru
Thanks for the help. It wasn't as hard as I thought it would be. ^^; I've been able to come up with the following so far:

Code:`// points.cpp`

#include <iostream.h>

int main()

{

int p[6] = {9,13,17,25,31,35};

int a, b, c, d, e, f, sum=0;

for (a = 0; a < 6; a++)

for (b = 0; b < 6; b++)

for (c = 0; c < 6; c++)

for (d = 0; d < 6; d++)

for (e = 0; e < 6; e++)

for (f = 0; f < 6; f++)

{

sum=p[a]+p[b]+p[c]+p[d]+p[e]+p[f];

if (sum == 100)

cout << p[a] <<" + "<< p[b] <<" + "<< p[c] <<" + "<< p[d] <<" + "<< p[e] <<" + "<< p[f] <<" = 100" << endl;

}

return 0;

}

1. How can I eliminate repeats? (ie: 35+25+9+9+9+13 and 35+25+9+9+13+9, and many others)

2. Is it possible to do the whole thing in one loop?