递归-8 全排列

//

//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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第1章 第一个C程序第2章 C语言基础第3章 变量和数据类型第4章 顺序结构程序设计第5章 条件结构程序设计第6章...
    小狮子365阅读 10,717评论 3 71
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,768评论 0 33
  • C语言的学习要从基础开始,这里是100个经典的算法-1C语言的学习要从基础开始,这里是100个经典的 算法 题目:...
    Poison_19ce阅读 1,166评论 0 0
  • 正门停车场不时有女人询问是否住宿。别想歪,这是家庭旅馆的揽客方式,景区内住宿农家,倒是最安全的。两天的门票,自然要...
    清和qinghe阅读 215评论 2 2
  • 一只狼受伤了可以找个山洞舔舐自己的血淋淋的皮毛,稍作休息就能恢复体力。同样,一个人受伤了也会找个角落自己独处。 可...
    酷酷的大敏阅读 441评论 3 2