Click to See Complete Forum and Search --> : Creating New Number from Some Numbers
vcstarter
November 28th, 2006, 08:06 AM
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?
Paul McKenzie
November 28th, 2006, 10:08 AM
http://www.codeguru.com/forum/showpost.php?p=721348&postcount=3
Sample main() program:
// 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
vcstarter
November 28th, 2006, 12:46 PM
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
Paul McKenzie
November 28th, 2006, 02:55 PM
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:
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.