Sudden whim to solve the permutation and combination problem in high school with C

Title: there are 1, 2, 3 and 4 numbers. How many different three digits can be formed without repeated numbers? How much is it?

A very simple high school topic is also well realized by program. Don't say much, just go to the code.

#include<stdio.h>
 
int main()
{
    int i,j,k;
    printf("\n");
    for(i=1;i<5;i++) { // The following is a triple cycle
        for(j=1;j<5;j++) {
            for (k=1;k<5;k++) { // Make sure i, j and k are different from each other
                if (i!=k&&i!=j&&j!=k) { 
                    printf("%d,%d,%d\n",i,j,k);
                }
            }
        }
    }
}

Because it is a three digit number, and the four numbers are still connected, we only need to have a triple cycle and remove those with the same number.

If it's a six digit or ten digit number, it's obviously inconvenient to cycle.

Moreover, if the number is 2, 4, 6, 8, it cannot even use the above cycle.

This is the problem I want to solve next. Since the basic idea is to cycle, can we complete an n-fold cycle? The answer is yes, you can look at my last article A ergodic algorithm for the change problem - "clock model"A ergodic algorithm for the change problem - "clock model" . It mentioned a traversal algorithm, similar to the clock, which can be used for n-cycles of continuous numbers. Of course, people will ask, what if it is not a continuous number? This is actually very easy to solve. Only a little mapping is needed. For example, there is an array {2,4,6,8}, although its elements are not continuous. But its subscripts are always continuous.

With the above analysis, we can start typing code.

First you have to know the "clock model"

while(1){
        b[n-1]--;
        for(j=n-1;j>=1;j--){
            if(b[j]==-1){
                b[j-1]--;
                b[j]=m[j];//m[n-1] is an array that stores the initial value of b[n-1], which is not written out here
            }
        }
        if(b[0]==-1) break;
	}

The second is how to correspond subscripts to array elements and output them. It is very simple to substitute an array whose elements are arranged in order into the above code, which is an ordinary arrangement and combination with order. Then output an array with the target number in the form of array1[array2[i]]. The attached code is easy to understand.

void fun1()
{
    int array1[4] = {2, 4, 6, 8};
    fun2(array1);
}

void fun2(int array1[], n, m)
{   
    int array2[m],mm[m];
    for(int i=0;i<m;i++) array2[i] = mm[i] = n-1;
    while(1){
        for(int i=0;i<n;i++){
            printf("%d",array1[array2[i]]);
        }
        printf("\n");
        array2[n-1]--;
        for(j=n-1;j>=1;j--){
            if(array2[j]==-1){
                array2[j-1]--;
                array2[j]=mm[j];
            }
        }
        if(array2[0]==-1) break;
    }
}

Almost all the basic principles have been written. As a classic high school topic, it sometimes has many restrictions. For example, the number of components should be from large to small, or from small to large. Without saying a word, it can be written directly into the code.

#include<stdio.h>

void onput1(int b[], int n, int ptr[], int m, int key);
void onput2(int b[], int n, int ptr[], int m, int key);
int dif(int array[], int n);//Determine whether the array has duplicate elements
int mima(int array[], int n);//Determine whether the element is greater than 0 
void input();

int dif(int array[], int n)
{
	int ii,jj,md=0;
	for(ii=0;ii<n-1;ii++){
		for(jj=ii+1;jj<n;jj++){
			if(array[ii]==array[jj]) md=1;
		}
		if(md==1) break;
	}
	
	return !md;
}

int mima(int array[], int n)
{
	int md=1;
	for(int ii=0;ii<n;ii++){
		if(array[ii]>9 || array<0) md=0;
	}
	
	return md;
}

void onput1(int b[], int n, int ptr[], int m, int key)
{
	int mm[n],nn[n],sum=0;
	for(int k=0;k<n;k++){
		mm[k]=b[k];
	}
	while(1){
		for(int i=0;i<n;i++){
			nn[i]=ptr[b[i]];
		}
		if(key==0 ? dif(nn, n) : 1 && ptr[b[0]]!=0){
			for(int i=0;i<n;i++){
	        	printf("%d",ptr[b[i]]);
			}
			printf("\n");
			sum++;
		}
        b[n-1]--;
        for(int j=n;j>=1;j--){
            if(b[j]==-1){
                b[j-1]--;
                b[j]=mm[j];
            }
        }
        if(b[0]==-1) break;
	}
	printf("Total%d Cases:",sum);
}

void onput2(int b[], int n, int ptr[], int m, int key)
{
	int mm[n],sum=0,result,l;
	for(int k=0;k<n;k++){
		mm[k]=b[k];
	}
	while(1){
		if(key==1){
			for(l=0;l<n-1;l++){
				if(ptr[b[l]]>=ptr[b[l+1]]) break;
			}
			if(l==n-1 && ptr[b[0]]!=0){
				for(int i=0;i<n;i++){
		        	printf("%d",ptr[b[i]]);
				}
				sum++;
				printf("\n");
			}
		}else if(key==2){
			for(l=0;l<n-1;l++){
				if(ptr[b[l]]<=ptr[b[l+1]]) break;
			}
			if(l==n-1 && ptr[b[0]]!=0){
				for(int i=0;i<n;i++){
		        	printf("%d",ptr[b[i]]);
				}
				sum++;
				printf("\n");
			}
		}
        b[n-1]--;
        for(int j=n;j>=1;j--){
            if(b[j]==-1){
                b[j-1]--;
                b[j]=mm[j];
            }
        }
        if(b[0]==-1) break;
	}
	printf("Total%d Cases:",sum);
}

void input()
{
	int n,m,*ptr,key=0;
	printf("Please enter the number of numbers required:");
	scanf("%d",&n);
	int array[n];
	printf("namely:");
	for(int i=0;i<n;i++){
		scanf("%d",&array[i]);
	}
	printf("To form several digits:");
	scanf("%d",&m);
	int b[m]; 
	ptr=array;
	for(int i=0;i<m;i++){
		b[i]=n-1;
	}
	if(dif(array, n) && mima(array, n)){
		printf("Arrange input 1 from small to large\n Input 2 from large to small\n No duplicate digital input 0\n Repeat numeric input 3 required\n-->");
		scanf("%d",&key);
		if(key==1){
			onput2(b, m, ptr, n, key);
		}else if(key==2){
			onput2(b, m, ptr, n, key);
		}else{
			onput1(b, m, ptr, n, key);
		}
	}else{
		printf("[ERROR]Data input error, please re-enter\n");
		input(); 
	} 
}

main()
{
	input();
}

And the results of my run

  

Well, my sharing is over. If you are interested, you might as well continue to modify this code by yourself so that it can solve more problems about high school permutation and combination.

Keywords: C Algorithm

Added by johncox on Thu, 06 Jan 2022 15:12:01 +0200