斐波那契数列:
#include <iostream>
using namespace std;
int factorial(int n)
{
if (n == 1)
{
return 1;
}
return n*factorial(n - 1);
}
int main()
{
int n;
cin >> n;
cout << factorial(n) << endl;
return 0;
}
汉诺塔(Hanoi):
#include <iostream>
using namespace std;
void hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
cout << n << " " << a << " " << c << endl;
}
else
{
hanoi(n - 1, a, c, b);
cout << n << " " << a << " " << c << endl;
hanoi(n - 1, b, a, c);
}
}
int main()
{
int n;
cout << "请输入汉诺塔层数(正整数):" << endl;
cin >> n;
cout << "汉诺塔运行过程:" << endl;
hanoi(n, 'A', 'B', 'C');