递归-2 汉诺塔

//

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

- (void)viewDidLoad {

[superviewDidLoad];

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

hanNuoTa('A','C','B',3);

}

voidhanNuoTa(chara,charc,charb,intn) {

if(n ==0) {

return;

}else{

//1.n-1从a->b借助c

hanNuoTa(a,b,c,n-1);

//把第n塔盘从a ->c

printf("把%d个塔盘从%c移动到%c \n",n,a,c);

//2.n-1从b->c借助a

hanNuoTa(b,c,a,n-1);

}

}

@end

你可能感兴趣的:(递归-2 汉诺塔)