練習問題/解答例/ハノイの塔/C++
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
*ハノイの塔 [#c68e3446]
#geshi(cpp){{
// Promblem
// hanoi
#include <iostream>
#include <string>
void solve(int x, std::string from, std::string to, std::...
static unsigned int counter = 0;
if(x == 1){
std::cout << ++counter << ": " << from << " to " << to ...
}else{
solve(x-1, from, work, to);
std::cout << ++counter << ": " << from << " to " << to ...
solve(x-1, work, to, from);
}
}
int main() {
const std::string FROM = "A";
const std::string TO = "B";
const std::string WORK = "C";
// Input the circle count
int x;
std::cout << "Input x > ";
std::cin >> x;
solve(x, FROM, TO, WORK);
return 0;
}
}}
終了行:
*ハノイの塔 [#c68e3446]
#geshi(cpp){{
// Promblem
// hanoi
#include <iostream>
#include <string>
void solve(int x, std::string from, std::string to, std::...
static unsigned int counter = 0;
if(x == 1){
std::cout << ++counter << ": " << from << " to " << to ...
}else{
solve(x-1, from, work, to);
std::cout << ++counter << ": " << from << " to " << to ...
solve(x-1, work, to, from);
}
}
int main() {
const std::string FROM = "A";
const std::string TO = "B";
const std::string WORK = "C";
// Input the circle count
int x;
std::cout << "Input x > ";
std::cin >> x;
solve(x, FROM, TO, WORK);
return 0;
}
}}
ページ名: