help with permutation problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: help with permutation problem

  1. #1
    Join Date
    Oct 2017

    Question 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. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: help with permutation problem

    [Moved from c++ as mainly an algorithm/computation issue]
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

  3. #3
    Join Date
    Feb 2017

    Re: help with permutation problem

    Quote Originally Posted by sam8868tw View Post
    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,

    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,
    Last edited by wolle; December 7th, 2017 at 01:57 AM.

  4. #4
    Join Date
    Oct 2017

    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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This a survey!

On-Demand Webinars (sponsored)