第二周:项目三(2)——汉诺塔

来源:互联网 发布:网通官方测速软件 编辑:程序博客网 时间:2024/06/05 01:15
#include <stdio.h>#define discCount 4long move(int, char, char,char);int main(){    long count;    count=move(discCount,'A','B','C');    printf("%d个盘子需要移动%ld次\n", discCount, count);    return 0;}long move(int n, char A, char B,char C){    long c1,c2;    if(n==1)        return 1;    else    {        c1=move(n-1,A,C,B);        c2=move(n-1,B,A,C);        return c1+c2+1;    }}

运行结果:

(1)discCount 4

(2)discCount  5

0 0
原创粉丝点击