//
//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"
intfirstCount =0;
intinputNum[3] = {0};
@interfaceViewController()
@end
@implementationViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//1.
[selftest1];
}
/**
【程序1】
題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?
*/
- (void)test1 {
intnum[4] = {1,2,3,4};
getThreeNum(0, num);
}
// index從0開始
voidgetThreeNum(intindex,int*num){
if(index ==3) {
printf("第%d個:", ++firstCount);
for(inti =0; i<3; i++) {
printf("%d",inputNum[i]);
}
printf("\n");
}else{
for(inti =1; i<=4; i++) {
//index以后的數據要清空
for(intk = index; k<3; k++) {
inputNum[k] =0;
}
//判斷下個書籍是否可以加入,index以前的來判斷
BOOLisCanJoinNum =YES;
//當前的將要放入的值在tempNum是存在的
for(intk =0; k
if(i ==inputNum[k]) {
isCanJoinNum =NO;
break;
}
}
if(isCanJoinNum) {
inputNum[index] = i;
getThreeNum(index+1, num);
}else{
continue;
}
}
}
}
@end
//更加簡單的辦法
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
intmap[5] = {0};
intsave[4] = {0};
test1(0,map,save);
}
/**
【程序1】
題目:有1、2、3、4個數字,能組成多少個互不相同且無重復數字的三位數?都是多少?
可以理解成3個桶A,B,C
在A桶的時候可以順序的放入1,2,3,4
在B桶中只能放入不在A桶的數(需要用一個Map數組來標記是否訪問過的)
在C桶中也是一樣的
*/
voidtest1(intstep,int* map,int* save) {
if(step ==3) {
for(inti =0; i <3; i ++) {
printf("%d",save[i]);
}
printf("\n");
return;
}
for(inti =1; i <=4; i ++) {//1,2,3,4
if(map[i] ==0) {
map[i] =1;//標記當前的i已經被用了不能再用了
save[step] = i;
test1(step+1,map,save);
//把當前的i解除占用,這樣在第二步的時候還是可以用這個i
map[i] =0;
}
}
}
@end