美文网首页算法
递归-8 全排列

递归-8 全排列

作者: 路灯下的黑猫H | 来源:发表于2017-03-16 15:47 被阅读0次

    //

    //ViewController.m

    //CocoTest_1

    //

    //Created by S u p e r m a n on 2017/3/14.

    //Copyright © 2017年张浩. All rights reserved.

    //

    #import"ViewController.h"

    staticintcount =0;

    @interfaceViewController()

    @end

    @implementationViewController

    - (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    inttemp[100] = {0};

    intn =5;

    intsum =1;

    inttempN = n;

    while(n>0) {

    sum = sum*n;

    n--;

    }

    printf("总共:%d\n",sum);

    fullArray(tempN,temp,0);

    }

    //n为输入的需要全排列的数temp为临时数组tempIndex为下标值

    voidfullArray(intn,int* temp,inttempIndex) {

    if(n ==0) {

    printf("count = %d: ",++count);

    for(inti =0; i

    printf(" %d ",temp[i]);

    }

    printf("\n");

    }else{

    for(inti =1; i<= n+tempIndex; i++) {

    //看是否可以放下一个值

    BOOLisCanJoinData =YES;

    for(intk =0; k<=tempIndex; k++) {

    if(i == temp[k]) {

    isCanJoinData =NO;

    break;

    }

    }

    if(isCanJoinData) {

    temp[tempIndex] = i;

    fullArray(n-1,temp,tempIndex+1);

    temp[tempIndex] =0;

    }

    }

    }

    }

    @end

    相关文章

      网友评论

        本文标题:递归-8 全排列

        本文链接:https://www.haomeiwen.com/subject/vldjnttx.html