CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: help with permutation problem

1. Junior Member
Join Date
Oct 2017
Posts
3

## help with permutation problem

How can I find the number of permutation of different types of objects?I want to do this with C++.Can anyone suggest the alogthrim for me?thank you!

example: There is 5 walls. I have 5 cans of Yellow paint, 3 cans of Red paint and 2 cans of Blue paint.How many possible painting solutions do I have?

2. ## Re: help with permutation problem

[Moved from c++ as mainly an algorithm/computation issue]

3. Member
Join Date
Feb 2017
Posts
379

## Re: help with permutation problem

Originally Posted by sam8868tw
How many possible painting solutions do I have?
What is a painting solution? Is the idea that one can of paint is precisely enough to paint one wall and you want to know all possible ways the walls can be colored?

Using your example, since one can is needed for each of the 5 walls the first step must be to find out in how many different ways one can pick out 5 cans. A set of 5 cans can have 0 to 5 cans of yellow, 0 to 3 cans of red and 0 to 2 cans of blue. Then to find out how a certain set of 5 cans can be used to paint the 5 walls you generate all permutations of that set.

There may be smarter ways but the simplest is to use three nested loops like this,

Code:
```for (int yellow=0; yellow<=5; ++yellow) {
for (int red=0; red<=3; ++red) {
for (int blue=0; blue<=2; ++blue) {
if (yellow+red+blue == 5) {
// 1. All different color combinations of 5 cans are available here one by one.
// 2. All permutations of each such color combination then give all ways
//     the 5 walls can be painted using that particular color combination.
}
}
}
}```
Fortunately there's a permutation function in the C++ standard library,

http://www.cplusplus.com/reference/a...t_permutation/
Last edited by wolle; December 7th, 2017 at 01:57 AM.

4. Junior Member
Join Date
Oct 2017
Posts
3

## Re: help with permutation problem

Thanks for your help! I will try to use your way to code.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width