當前位置:首頁 » 編程語言 » 漢諾塔問題c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

漢諾塔問題c語言

發布時間: 2022-01-14 16:54:48

c語言漢諾塔問題

哥們,你還沒明白什麼叫遞歸,用tc單步執行一遍看看你就會明白了,這里說太麻煩。

⑵ C語言漢諾塔問題,不是很理解

請注意,void hanoi ( int n, char a, char b, char c )
這里的a,b,c是三個char變數。
當執行調用hanoi(3,'B','C','A');時,「將a柱子上的最後一個盤子移動到c」,
其中的a的值就是'B',c的值就是'A',也就是「將B柱子上的最後一個盤子移動到A柱」了。

⑶ c語言遞歸問題: 漢諾塔問題:

//可運行的代碼
#include <stdio.h>
void move(char x,char y) // 定義move函數
{
printf("%c-->%c\n",x,y);
}

void hannuota(int n,char one,char two,char three) // 定義hanuota函數
// 將n個盤從one座藉助two座,移到three座
{

if(n==1)
move(one,three);
else
{
hannuota(n-1,one,three,two);
move(one,three);
hannuota(n-1,two,one,three);
}
}

int main()
{
int m;
printf("input the number of diskes:");
scanf("%d",&m);
printf("The step to move %d diskes:\n",m);
hannuota(m,'A','B','C');
while(1);
return 0;
}
/*
input the number of diskes:3
The step to move 3 diskes:
A-->C
A-->B
C-->B
A-->C
B-->A
B-->C
A-->C
*/

⑷ C語言漢諾塔問題的疑惑

親,這不是很簡單嗎。一看就知道是ABC三根柱子,然後A柱上有三個盤,把A柱子上的盤套到C柱子上,另外,你這程序編的也不是很完美,我把一個更完美的HAONI塔的演算法給你參考下!!!(*^__^*)
==========================================================================
#include <stdio.h>
void hanoi(int n, char A, char B, char C) {
if(n == 1) {
printf("Move sheet %d from %c to %c\n", n, A, C);
}
else {
hanoi(n-1, A, C, B);
printf("Move sheet %d from %c to %c\n", n, A, C);
hanoi(n-1, B, A, C);
}
}
int main() {
int n;
printf("請輸入盤數:");
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
return 0;
}

⑸ 漢諾塔問題c語言,哪錯了

#include<stdio.h>
void fun(int n,char dish_a,char dish_b,char dish_c)
{
if(n==1)printf("%c-->%c\n",dish_a,dish_c); //問題一
else //問題二
{
fun(n-1,dish_a,dish_c,dish_b);
fun(1,dish_a,dish_b,dish_c);
fun(n-1,dish_b,dish_a,dish_c);
}
}
void main()
{
int n;
char A,B,C;
scanf("%d",&n);
{
fun(n,'A','B','C');
getchar();
}
}

⑹ c語言:求解漢諾塔問題

漢諾塔問題用遞歸解決比較容易,下面代碼是我用C++寫的,你可以參考
#include <iostream>
using namespace std;

void Move(char ch1,char ch2)
{
cout<<ch1<<"------->"<<ch2<<endl;
}
void Hannuo(int n,char PA,char PB,char PC)
{
if (n==1)
{
Move(PA,PC);
return;
}
else {
Hannuo(n-1,PA,PC,PB);
Move(PA,PC);
Hannuo(n-1,PB,PA,PC);
}
}
void main()
{ int N;
cout<<"請輸入你要移動的盤子數:";
cin>>N;
Hannuo(N,'A','B','C');
}

⑺ C語言!!!漢諾塔問題

你是不理解1,2,3為啥換來換去是嗎?

⑻ 漢諾塔C語言問題

我初學時,也看不懂,主要是不理解遞歸思想,好好看看,你按著這個演算法用筆畫一下每個步驟後的結果,畫到最後就看出來是什麼回事了。

⑼ c語言問題的漢諾塔問題

你的想法是:A-B B-C A-C A-C A-B C-B
由於小的在上A-B B-C 之後C上已經有盤子了
所以接下來的你的步驟中的A-C就不能進行了