算法之万能的搜索(c语言实现和js实现)

2017-03-13  本文已影响0人  码太农

//
// main.c
// 万能的搜索
//
// Created by yali on 17/3/13.
// Copyright © 2017年 liyu. All rights reserved.
//

include <stdio.h>

int a[10],book[10],n;
void dts(int step){
int i;
if (step ==n+1) {

    for (i=1; i<=n; i++) {
        printf("%d",a[i]);
    }
    printf("\n");
    
    return ;
}


for (i=1; i<=n; i++) {
    if (book[i]==0) {
        a[step] =i;
        book[i]=1;
        
        dts(step+1);
        book[i]=0;
    }
}
return ;

}

int main(int argc, const char * argv[]) {
// insert code here...
// printf("Hello, World!\n");
scanf("%d",&n);
dts(1);
getchar();getchar();
return 0;
}

//下面是js的实现过程,很简单,理解
<script>

var a =[];
var book= [0,0,0,0];
function f1(step) {

    if (step == 4) {

        for(var j=1;j <=3;j++) {
           document.write(a[j]);


        }
        return;
    }


    for (var i = 1; i <= 3; i++) {
        if (book[i] == 0) {
            a[step]=i;
            book[i]=1;


            f1(step+1);
            book[i]=0;

        }
    }
    return;
}
f1(1);

</script>

上一篇下一篇

猜你喜欢

热点阅读