Permutation and combination summary: how to output the results

  • 2020-04-01 23:26:51
  • OfStack

Full array output:

Solution a:


<SPAN style="COLOR: #333333">#include <stdio.h>

void swap(int a[],int size)
{
 int i,t;
 if(size==0)
 {
  for(i=0;i<5;i++)
  {
   printf("%c ",a[i]);
  }
  printf("n");
  return;
 }
 else
 {
  for(i=0;i<=size;i++)  //The element loops all the way through
  {   
   //Before swap(), pass the value recursively & NBSP; The exit point is passed in a function
   t=a[i];a[i]=a[size];a[size]=t;  
   swap(a,size-1);
   //Array element restore & NBSP; What was the beginning now or what position has changed to become the original position & NBSP;
   //Easy to switch from a[1] to a[n] with the last element
   t=a[i];a[i]=a[size];a[size]=t;  
  }
 }
}
int main()
{
 int a[5],i;
 for(i=0;i<5;i++)
 {
  a[i]=97+i;
 }
 swap(a,4);
 //printf("n%d",m);
 return 0;
}</SPAN>

Method 2:


<SPAN style="COLOR: #333333">#include <stdio.h>
//Ideas & have spent Find the full arrangement of the remaining Numbers beginning with 1, 2, 3, 4, and 5 respectively & PI; All the way down to a number
void swap(int a[],int k)
{
 int i,m,t=0;
 if(k==5)  
 {
  for(i=0;i<5;i++)
  {
   printf("%d ",a[i]);
  }
  //k++;
  printf("n");
 }
 for(i=k;i<5;i++)
 {
  {m=a[k];a[k]=a[i];a[i]=m;}
  swap(a,k+1);
  {m=a[k];a[k]=a[i];a[i]=m;}
 }
}
int main()
{
 int a[5]={1,2,3,4,5};  //The number of recursive operations
 swap(a,0);  //A function call
    return 0;
}
</SPAN>

N of m Numbers are arranged:


#include <stdio.h>
void swap(int a[],int b[],int i,int size)
{
 int k,j,temp;
 if(i==3)
 {
   for(k=0;k<3;k++)
   {
    printf("%d ",b[k]);
   }
   printf("n");
  return;
 }
 else
 {
  for(j=0;j<size;j++)
  {
   b[i]=a[j];
   temp=a[j];a[j]=a[size-1];a[size-1]=temp;
   swap(a,b,i+1,size-1);
   temp=a[j];a[j]=a[size-1];a[size-1]=temp;
  }
 }
}
int main()
{
 int a[5]={1,2,3,4,5},b[3];//Find all the permutations of three of the five Numbers
    swap(a,b,0,5);
 return 0;
}

M number n to combine:

[10 reverse substitution method]

Algorithm idea:

        (1)   Initializes an array of m elements (all composed of 0, 1), initializing the first n elements to 1 and the last to 0. At this point you can output the first sequence of combinations.
        (2)   I'm going to go front to back, I'm going to go to the first 10, and I'm going to flip it to 01, and then I'm going to push all the 1's in front of the 10 to the left, so I'm going to keep all the 1's in front of it to the left. At this point, you can output groups of grouped sequences.
        (3)   Repeat step (2) until you cannot find the 10 combo positions. All the possibilities have been output


#include <stdio.h>
#include <stdlib.h>
void putout(int * num,int m)
{
 int i;
 for(i=0;i<m;i++)
 {
  if(*(num+i))
   printf("%d ",i+1);

 }
 printf("n");
}
int check(int *num,int m,int n)
{
 int flag=1,i;//When flag=1, continue the while loop and exit the loop
    for(i=0;i<m-n;i++)
 {
  if(*(num+i))
  {
          return  1;
  }
 }
 return 0;
}
void choseNum(int *num,int m,int n)
{
 int i,j;
    putout(num,m);  //Output the first combination
 while(1)
 {
  int count=0;  //Notice the count position & cake; He's the only one who's been debugging
  //Find the first combination of 1 and 0
  for(i=0;i<m-1;i++)
  {
   if(*(num+i)==1&&*(num+i+1)==0)
   {
    *(num+i)=0;
    *(num+i+1)=1;
    break;
   }
   if(*(num+i))  //Count the number of times 1 appears before
   count++; 
  }
  for(j=0;j<i;j++)
  {
   if(j<count)  //I'm going to make all of the first Numbers 1
   {
    *(num+j)=1;
   }
   else       //The last few Numbers are 0
   {
    *(num+j)=0;
   }
  }
  putout(num,m);
  if(check(num,m,n)!=1)
   break;
 }
 free(num);
}

int main()
{
 int m,n;//Find n combinations of m Numbers
 printf(" from m The number of n Combination of Numbers: ");
 scanf("%d %d",&m,&n);
    int *num,i;
 //int count;
 num=(int *)malloc(sizeof(int)*m);
 for(i=0;i<m;i++)
 {
  if(i<n)
   *(num+i)=1;
  else
   *(num+i)=0;
 }
 choseNum(num,m,n);
    return 0;
}

Examples of results:

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201305/20130508160734.png ">


Related articles: