0
3つのパラメータを持つハノイ再帰アルゴリズムを記述する必要があります。これは私が得たものである:私の塔のハノイアルゴリズムがうまくいかないのはなぜですか?
#include <stdio.h> #include <stdio.h>
void hanoi(int m, int i, int j);
void move(int start, int end);
int main(){
int n = 0;
int i = 1;
int j = 3;
printf("Enter how many disks you want to move: ");
scanf("%d", &n);
int m = n;
hanoi(m,i,j);
}
void hanoi(int m, int i, int j){
if (m==1)
{
move(i,j);
return;
}
hanoi(m-1,i,2);
move(i,j);
hanoi(m-1,2,j);
}
void move(int start, int end){
printf("A disk moves from position %d. to %d.\n", start,end);
}
は、n = 3の出力は以下の通りです:
Enter how many disks you want to move: 3
A disk moves from position 1. to 2.
A disk moves from position 1. to 2.
A disk moves from position 2. to 2.
A disk moves from position 1. to 3.
A disk moves from position 2. to 2.
A disk moves from position 2. to 3.
A disk moves from position 2. to 3.
私はgoold古いハノイの問題を解決するためのそれらのトンがある知っている、他のアルゴリズムを見て。しかし、それらはすべて4つのパラメータを持ちキャラクタを使用していますが、私は数字だけを使用して補助タワーパラメータを自分の関数に残したいと思います。どうすれば修正できますか? n = 1、n = 2の場合、アルゴリズムは正常に動作します。
でコメントとして記述的な変数名を使用してくださいというよりも
2
、他ペグを使用する必要があります。 –問題は '2'がハードコードされていることです。 –
@KarolyHorvath私が見る...私はそれをバイパスできますか?関数の最初に値2の変数を挿入すると解決しません。:/ – SoflolikeAntonio