C language to achieve mathematical composition and arrangement

Source: Internet
Author: User

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--)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.