递归-2 汉诺塔

作者: 路灯下的黑猫H | 来源:发表于2017-03-16 15:27 被阅读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"

- (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 汉诺塔

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