体验复杂度—汉诺塔

/*
*Copyright (c) 2014,烟台大学计算机控制工程学院
*All right reserved
*文件名称:test.cpp
*作    者:邱暖
*完成日期:2015年9月14日
*版本号 :v1.10
*问题描述:交换两个整型变量
*/
(1)4个盘子、
#include <stdio.h>
#define discCount 4
long 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;
    }
}
运行结果:
<img src="http://img.blog.csdn.net/20150914172711041?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="512" height="315" alt="" />
(2)8个盘子
#include <stdio.h>
#define discCount 8
long 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;
    }
}
运行结果:
<img src="http://img.blog.csdn.net/20150914172908771?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="565" height="344" alt="" />
(3)16个盘子
#include <stdio.h>
#define discCount 16
long 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;
    }
}
运行结果:
<img src="http://img.blog.csdn.net/20150914173104455?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="548" height="281" alt="" />
(4)20个盘子
#include <stdio.h>
#define discCount 20
long 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;
    }
}
运行结果:
<img src="http://img.blog.csdn.net/20150914173223422?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="507" height="316" alt="" />
(5)24个盘子
#include <stdio.h>
#define discCount 24
long 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;
    }
}
运行结果:
<img src="http://img.blog.csdn.net/20150914173356630?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
学习心得:
    随着n的数值变大,复杂度越来越复杂。


你可能感兴趣的:(体验复杂度—汉诺塔)