程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> VC++完成模仿漢諾塔後果

VC++完成模仿漢諾塔後果

編輯:關於C++

VC++完成模仿漢諾塔後果。本站提示廣大學習愛好者:(VC++完成模仿漢諾塔後果)文章只能為提供參考,不一定能成為您想要的結果。以下是VC++完成模仿漢諾塔後果正文


先上後果圖

再附上源代碼:

漢諾塔:


#include "stdio.h"
#include "math.h"
int arrA[15], arrB[15], arrC[15];   // 分離為A、B、C
int length;
int lenA, lenB, lenC;
char plate[32];
// Make
void makeplate(int n)
{
    int i;
    if (n == length + 1)
    {
        for (i = 0; i < 2 * length + 3; i++)
        {
            if (i == length + 1)
            {
                plate[i] = '|';
            }
            else
            {
                plate[i] = '_';
            }
        }
    }
    else
    {
        if (n == 0)
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    plate[i] = ' ';
                }
            }
        }
        else
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    if (i >= length + 1 - n && i <= length || i > length + 1
                        && i <= length + 1 + n)
                    {
                        plate[i] = '_';
                    }
                    else
                    {
                        plate[i] = ' ';
                    }
                }
            }
        }
    }
    plate[i] = '\0';
}
// Draw
void drawtower()
{
    int i;
    printf(" ");
    for (i = length; i >= 0; i--)
    {
        if (i <= lenA)
        {
            makeplate(arrA[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenB)
        {
            makeplate(arrB[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenC)
        {
            makeplate(arrC[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        printf("\n ");
    }
}
// Move
void moveplate(int n, char x, char y)
{
    int i, j;
    if (x == 'A')
    {
        lenA--;
    }
    else
    {
        if (x == 'B')
        {
            lenB--;
        }
        else
        {
            lenC--;
        }
    }
    if (y == 'A')
    {
        lenA++;
        arrA[lenA] = n;
    }
    else
    {
        if (y == 'B')
        {
            lenB++;
            arrB[lenB] = n;
        }
        else
        {
            lenC++;
            arrC[lenC] = n;
        }
    }
    drawtower();                // 繪出挪動一次後漢諾塔的狀況
}
// Print And Move
void printandmove(int n, char x, char y)
{
    printf("\n %d 號盤從 %c 柱移到 %c 柱\n\n", n, x, y);
    moveplate(n, x, y);
}
// Hanoi
void hanoi(int n, char one, char two, char three)
{
    if (n == 1)
    {
        printandmove(n, one, three);
    }
    else
    {
        hanoi(n - 1, one, three, two);
        printandmove(n, one, three);
        hanoi(n - 1, two, one, three);
    }
}
// Main
void main()
{
    int n, i;                   // n為漢諾塔盤子數,如要轉變,只需更改初始值便可。
    char one = 'A', two = 'B', three = 'C';
    printf("請輸出盤子個數[1—12]:");
    scanf("%d", &n);
    if (n >= 1 && n <= 12)
    {
        length = n;
        lenA = n;
        for (i = 0; i <= lenA; i++)
        {
            arrA[i] = n + 1 - i;
        }
        lenB = lenC = 0;
        arrB[0] = arrC[0] = n + 1;
        printf(" 漢諾塔模仿挪動進程[%d個盤]\n\n", n);
        drawtower();            // 繪出漢諾塔初始狀況
        hanoi(n, one, two, three);
        printf("\n 模仿停止,共挪動%ld次\n", (long)pow(2, n) - 1);
    }
    else
    {
        printf("數據毛病!\n");
    }
}

漢諾塔.c


/* 漢諾塔模仿
   2013-5-13
*/
#include "stdio.h"
#include "math.h"
int arrA[15], arrB[15], arrC[15];   // 分離為A、B、C
int length;
int lenA, lenB, lenC;
char plate[32];
// Make
void makeplate(int n)
{
    int i;
    if (n == length + 1)
    {
        for (i = 0; i < 2 * length + 3; i++)
        {
            if (i == length + 1)
            {
                plate[i] = '|';
            }
            else
            {
                plate[i] = '_';
            }
        }
    }
    else
    {
        if (n == 0)
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    plate[i] = ' ';
                }
            }
        }
        else
        {
            for (i = 0; i < 2 * length + 3; i++)
            {
                if (i == length + 1)
                {
                    plate[i] = '|';
                }
                else
                {
                    if (i >= length + 1 - n && i <= length || i > length + 1
                        && i <= length + 1 + n)
                    {
                        plate[i] = '_';
                    }
                    else
                    {
                        plate[i] = ' ';
                    }
                }
            }
        }
    }
    plate[i] = '\0';
}
// Draw
void drawtower()
{
    int i;
    printf(" ");
    for (i = length; i >= 0; i--)
    {
        if (i <= lenA)
        {
            makeplate(arrA[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenB)
        {
            makeplate(arrB[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        if (i <= lenC)
        {
            makeplate(arrC[i]);
            printf("%s", plate);
        }
        else
        {
            makeplate(0);
            printf("%s", plate);
        }
        printf("\n ");
    }
}
// Move
void moveplate(int n, char x, char y)
{
    int i, j;
    if (x == 'A')
    {
        lenA--;
    }
    else
    {
        if (x == 'B')
        {
            lenB--;
        }
        else
        {
            lenC--;
        }
    }
    if (y == 'A')
    {
        lenA++;
        arrA[lenA] = n;
    }
    else
    {
        if (y == 'B')
        {
            lenB++;
            arrB[lenB] = n;
        }
        else
        {
            lenC++;
            arrC[lenC] = n;
        }
    }
    drawtower();                // 繪出挪動一次後漢諾塔的狀況
}
// Print And Move
void printandmove(int n, char x, char y)
{
    printf("\n %d 號盤從 %c 柱移到 %c 柱\n\n", n, x, y);
    moveplate(n, x, y);
}
// Hanoi
void hanoi(int n, char one, char two, char three)
{
    if (n == 1)
    {
        printandmove(n, one, three);
    }
    else
    {
        hanoi(n - 1, one, three, two);
        printandmove(n, one, three);
        hanoi(n - 1, two, one, three);
    }
}
// Main
void main()
{
    int n, i;                   // n為漢諾塔盤子數,如要轉變,只需更改初始值便可。
    char one = 'A', two = 'B', three = 'C';
    printf("請輸出盤子個數[1—12]:");
    scanf("%d", &n);
    if (n >= 1 && n <= 12)
    {
        length = n;
        lenA = n;
        for (i = 0; i <= lenA; i++)
        {
            arrA[i] = n + 1 - i;
        }
        lenB = lenC = 0;
        arrB[0] = arrC[0] = n + 1;
        printf(" 漢諾塔模仿挪動進程[%d個盤]\n\n", n);
        drawtower();            // 繪出漢諾塔初始狀況
        hanoi(n, one, two, three);
        printf("\n 模仿停止,共挪動%ld次\n", (long)pow(2, n) - 1);
    }
    else
    {
        printf("數據毛病!\n");
    }
}

以上所述就是關於VC++完成漢諾塔後果的全體代碼了,願望對年夜家懂得漢諾塔算法可以或許有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved