Today want to use the C language to achieve the combination of mathematics and arrangement, has been very depressed.
Search the Internet for an example:
#include <stdio.h>
#include <stdlib.h>
From the array A of n elements, take the combination of M elements
BOOL Zuhe (char a[],int n,int m)
{The first X element that//p[x]=y takes is the y element of a
int index,i,*p;
p= (int*) malloc (sizeof (int) *m);
if (p==null)
{
return false;
}
index=0;
p[index]=0;//take the first element
while (true)
{
if (p[index]>=n)
{//Take the end, rewind
if (index==0)
{//All kinds of cases have been completed, can not be returned back
Break
}
index--;//back to the previous
p[index]++;//substitution Element
}
else if (index==m-1)
{//take enough, output
for (i=0;i<m;i++)
{
printf ("%c", A[p[i]);
}
printf ("\ n");
p[index]++; Replace element
}
Else
{//Take one more element
index++;
p[index]=p[index-1]+1;
}
}
Free (p);
return true;
}
Array A for n elements, all arranged
BOOL Pailie (char a[],int N)
{The first X element that//p[x]=y takes is the y element of a
int i,j,temp,*p;
p= (int*) malloc (sizeof (int) *n);
if (p==null)
{
return false;
}
for (i=0;i<n;i++)
{//initial arrangement
P[i]=i;
}
while (true)
{//Cycle m=n! times
Output a sort of arrangement
for (i=0;i<n;i++)
{
printf ("%c", A[p[i]);
}
printf ("\ n");
Look forward from the back to see if there are more than the number of the previous case, if there is a stop in the last number of positions.
For (i=n-1;i>0 && p[i]<p[i-1];i--);
If there is no more than the number of the previous number, the description has been to the last arrangement, return
if (i==0) break;
From the back to I, look for the smallest number greater than p[i-1], credited to J
For (J=n-1;j>i && p[j]<p[i-1];j--);
Exchange P[i-1] and P[j]
TEMP=P[I-1];p [i-1]=p[j];p [j]=temp;
Inverted p[i] to p[n-1]
for (i=i,j=n-1;i<j;i++,j--)