斐波那契额数列:
递推:
#include<iostream>
using namespace std;
int main() {
int a[100];
a[1] = 0, a[2] = 1;
for (int i = 3; i <= 10; i++) {
a[i] = a[i - 1] + a[i - 2];
}
}
递归:
#include<iostream>
using namespace std;
int func(int n) {
if (n == 1) { return 0; }
if (n == 2) { return 1; }
return func(n - 1) + func(n - 2);
}
int main() {
cout<<func(10)<<endl;
}
四座汉诺塔:
#include<iostream>
#include<cstring>
using namespace std;
int a[15], f[15];
int main(void)
{
a[1] = 1;
for (int i = 2; i <= 12; ++i)
a[i] = 1 + a[i - 1] * 2;
memset(f, 0x3f, sizeof(f));
f[0] = 0;
for (int i = 1; i <= 12; ++i)
{
for (int j = 0; j < i; ++j)
f[i] = min(f[i], f[j] * 2 + a[i - j]);
}
for (int i = 1; i <= 12; ++i)
cout << f[i] << endl;
return 0;
}