资源描述:
《汉诺塔问题的ppt详解.ppt》由会员上传分享,免费在线阅读,更多相关内容在PPT专区-天天文库。
1、//汉诺塔ABCABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔ABC//汉诺塔//汉诺塔#includevoidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c”,a,c);hanoi(n-1,b,a,c);}}voidmain(){intn;printf("Input
2、thenumberofdiskes:“);scanf(“%d”,&n);hanoi(n,'A','B','C');}StacknabcOutput//汉诺塔voidmain(){intn;printf("Inputthenumberofdiskes:“);scanf(“%d”,&n);hanoi(n,'A','B','C');}StacknabcH(3,A,B,C)3ABCOutputH(n,A,B,C)//汉诺塔voidmain(){intn;printf("Inputthenumbe
3、rofdiskes:“);scanf(“%d”,&n);hanoi(n,'A','B','C');}StacknabcH(3,A,B,C)3ABCOutputH(n,A,B,C)voidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}//汉诺塔StacknabcH(3,A,B,C)3ABC2ACB1ABCOutputH(n,A,B,C)H(n-
4、1,a,c,b)H(n-1,a,c,b)H(0,A,C,B)H(n-1,a,c,b)0ACBH(2,A,C,B)H(1,A,B,C)voidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,C)3ABCH(2,A,C,B)2ACBH(1,A,B,C)1ABCOutputH(n,A,B,C)H(n-1,a,c,b)H(
5、n-1,a,c,b)H(0,A,C,B)H(n-1,a,c,b)0ACBvoidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,C)3ABCH(2,A,C,B)2ACBH(1,A,B,C)1ABCOutputACH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)coutvoidhanoi(int
6、n,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,C)3ABCH(2,A,C,B)2ACBH(1,A,B,C)1ABCOutputACH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)H(0,B,A,C)H(n-1,b,a,c)0BACvoidhanoi(intn,chara,charb,charc){if(n>=
7、1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,C)3ABCH(2,A,C,B)2ACBH(1,A,B,C)1ABCOutputACH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)H(0,B,A,C)H(n-1,b,a,c)0BACvoidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(
8、“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,C)3ABCH(2,A,C,B)2ACBH(1,A,B,C)1ABCOutputACH(n,A,B,C)H(n-1,a,c,b)H(n-1,a,c,b)voidhanoi(intn,chara,charb,charc){if(n>=1){hanoi(n-1,a,c,b);printf(“%c-->%c“,a,c);hanoi(n-1,b,a,c);}}StacknabcH(3,A,B,