CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    Creating New Number from Some Numbers

    Assume that I have the following number

    {1, 2, 3, 4, 5, 6};

    I want to create the following unique number with space between each of them

    so I should have a result similar to that; the last number must be unique, the order does not matter; I want to have all possibilities

    1 2 3 4 5 6;
    1 2 3 5 6 1;
    1 2 3 5 6 2;
    1 2 3 5 6 3;
    1 2 3 5 6 4;
    1 2 3 5 6 5;

    The set above is not that good, but this one is better; assume that I have
    {2, 3, 8, 9, 10, 11, 22, 30}; whathever how they are given, the result should be in the order of 6

    from the set above, I can have a result like this
    2 3 8 9 10 11;
    2 3 10 11 30 22;
    etc.

    I want to have all the possibilities for the combinition of 6

    How can I do that?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Creating New Number from Some Numbers

    http://www.codeguru.com/forum/showpo...48&postcount=3

    Sample main() program:
    Code:
    // Assume combo class has been included...
    //...
    #include <vector>
    using namespace std;
    
    int main()
    {
        int array [] = {2, 3, 8, 9, 10, 11, 22, 30};
        int numelements = sizeof(array) / sizeof(array[0]);
        vector<int> elements(array, array + numelements);
        vector<int> results(elements.size());
    
        // Here is a Combination Generator instrance that handles integers
        // This is C(8, nItems) (8 items taken nItems at a time)
        int nItems = 6;
        CombinationGenerator<int> CG(elements, nItems);
    
        // Call NextCombination until there are no more combinations
        int nTries = 0;
        while (CG.NextCombination(results))
        {
            // Output our results
            int nSize = results.size();
            for ( int j = 0; j < nSize; j++ )
                    cout << results[j] << " ";
            cout << endl;
            nTries++;
        }
        cout << "Number of combinations is " << nTries << endl;
    }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 28th, 2006 at 11:17 AM.

  3. #3
    Join Date
    Jun 2002
    Posts
    936

    Re: Creating New Number from Some Numbers

    There is a discrepancy with the number. Assume that the given number is 5 number, the output show 1 combination. That is not the case, I assume the output will be 6 combinations, for example assume that

    x[] = {1, 2, 3, 4, 5, 6}
    the output should be
    1 2 3 4 5 6;
    1 2 3 4 6 5;
    1 2 3 6 5 4;
    1 2 6 5 4 3;
    2 6 5 4 3 1;

    In order to get that, I was thingking about something like that

    1. Put the new number x to a vector
    2. Sort the vector
    3. Get the first 5 element and append the last one at the end of it
    4. Leave the first one and work with the next 5
    5. and keep repeating

    that may not be good, but how to get all the numbers from the set

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Creating New Number from Some Numbers

    Quote Originally Posted by vcstarter
    There is a discrepancy with the number. Assume that the given number is 5 number, the output show 1 combination.
    I'm trying to decipher what you're saying, but I am having a hard time doing so.

    What is the difference between a simple combination of r things taken from n items, and what you're trying to do?

    Also, the program I posted, when run with your data produces this:
    Code:
    2 3 4 5 6
    1 3 4 5 6
    1 2 4 5 6
    1 2 3 5 6
    1 2 3 4 6
    1 2 3 4 5
    Number of combinations is 6
    6 combinations.

    Regards,

    Paul McKenzie

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

Featured