递归-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