Sunday, February 20, 2011

WAP to Print Compbinations e.g combination of n things taken k at a time without or with repetitions

/*In mathematics a combination is a way of selecting several things out of a larger group, where (unlike permutations) order does not matter. In smaller cases it is possible to count the number of combinations. For example given three fruit, an apple, orange and pear say, there are three combinations of two that can be drawn from this set: an apple and a pear; an apple and an orange; or a pear and an orange. More formally a k-combination of a set S is a subset of k distinct elements of S. If the set has n elements the number of k-combinations is equal to the binomial coefficient

\binom nk = \frac{n(n-1)\ldots(n-k+1)}{k(k-1)\dots1},

which can be written using factorials as \frac{n!}{k!(n-k)!} whenever k\leq n, and which is zero when k > n. The set of all k-combinations of a set S is sometimes denoted by

\binom Sk\,.

Combinations can consider the combination of n things taken k at a time without or with repetitions.
*/

# include
using namespace std;
bool used[256]={0}; //Concept nCr
int r=3;
char a[] = "ABCD";
//int length=strlen(pIn);
void printOutPutArray(char *out,int outIndex){
for(int i=0;i cout<<"\n";
}

void combinations(char *out, int outIndex, int included,int length)
{
if (included==r) {printOutPutArray(out,outIndex); return;
}
for (int i=0;i{
if (used[i]) continue;
out[outIndex++] = a[i];
used[i]=1;
combinations(out, outIndex, included+1,length);
used[i]=0;
outIndex--;
}

}

//st above functions */
int main()
{
char out[100];
combinations(out, 0, 0,4);
return 0;
}

No comments :