Full alignment (C language)

Full array problem

     Any number of allowed sequence of all distinct arranged to output a natural number n, i.e., n whole arrangement requires the generated duplicate numbers appear.

             / * Problem-solving ideas:

             Assuming that number is licensing, is equivalent to sort cards into the box inside, in order to solve problems

             */

#include<stdio.h>
int a[10],book[10]={0};//a代表盒子,book代表手上的牌 
int n;//n代表1~n 
void dfs(int step){
    int i;
    if(step==n){
        for(i=0;i<n;i++){
            printf("    %d",a[i]);
        }
        printf("\n");
        return;
    }
    for(i=0;i<n;i++){
        if(book[i]==0){
            a[step]=i+1;//将手上的牌放进盒子中去 
            book[i]=1;//表示牌已经不在手上 
            dfs(step+1);//递归 
            book[i]=0;//如果返回,就将牌从盒子里面抽出 
        }
    }
}
int main(){
    scanf("%d",&n);
    dfs(0);
    return 0;
}
Published 34 original articles · won praise 6 · views 4774

Guess you like

Origin blog.csdn.net/qq_42712280/article/details/100661216