将上题用C++
```cpp #include <iostream> using namespace std; long long moves = 0; void hanoi(int n, char source, char target, char auxiliary) { if (n == 1) { cout << source << "->1->" << target << endl; moves++; } else { hanoi(n - 1, source, auxiliary, target); cout << source << "->" << n << "->" << target << endl; moves++; hanoi(n - 1, auxiliary, target, source); } } int main() { int N; cin >> N; moves = 0; hanoi(N, 'a', 'c', 'b'); cout << moves << endl; return 0; } ``` [2026-01-05 17:53:37 | AI问答 | 165点数解答]
相关提问