private/C言語基礎/解答例13A-4
をテンプレートにして作成
開始行:
*ハノイの塔 [#l6d0aacc]
#geshi(c){{
/*
* ハノイの塔を解く
* fromにあるn枚の円盤を、withを利用してtoへ移動させる
*/
void hanoi(int n; char from, char with, char to)
{
n > 1 && hanoi(n - 1, from, with, to); // n-1の塔をfro...
printf("円盤%dを%cから%cへ移動させる\n", n, from, to);
n > 1 && hanoi(n - 1, with, to, from); // n-1の塔をwit...
}
}}
#geshi(c){{
void hanoi(int n; char from, char to, char with)
{
// fromにあるn-1枚の円盤を、toを利用してwithへ移動し退...
if (n > 1) { hanoi(n - 1, from, with, to); }
printf("円盤%dを%cから%cへ移動させる\n", n, from, to);
// withに退避しておいたn-1枚の円盤を、fromを利用してto...
if (n > 1) { hanoi(n - 1, with, to, from); }
}
}}
#geshi(c){{
int main(void) {
hanoi(5, 'A', 'B', 'C'); // Aにある5枚の円盤を、Cを利...
return 0;
}
}}
終了行:
*ハノイの塔 [#l6d0aacc]
#geshi(c){{
/*
* ハノイの塔を解く
* fromにあるn枚の円盤を、withを利用してtoへ移動させる
*/
void hanoi(int n; char from, char with, char to)
{
n > 1 && hanoi(n - 1, from, with, to); // n-1の塔をfro...
printf("円盤%dを%cから%cへ移動させる\n", n, from, to);
n > 1 && hanoi(n - 1, with, to, from); // n-1の塔をwit...
}
}}
#geshi(c){{
void hanoi(int n; char from, char to, char with)
{
// fromにあるn-1枚の円盤を、toを利用してwithへ移動し退...
if (n > 1) { hanoi(n - 1, from, with, to); }
printf("円盤%dを%cから%cへ移動させる\n", n, from, to);
// withに退避しておいたn-1枚の円盤を、fromを利用してto...
if (n > 1) { hanoi(n - 1, with, to, from); }
}
}}
#geshi(c){{
int main(void) {
hanoi(5, 'A', 'B', 'C'); // Aにある5枚の円盤を、Cを利...
return 0;
}
}}
ページ名: