费纳波契数列
- dp[]数组实现
const int maxn = 1e5 + 6;
ll dp[maxn];
int main() {
int n;
sc(n);
dp[0] = dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
printf("%lld",dp[n]);
}
- 滚动数组实现
int main() {
int n;
sc(n);
ll a = 1, b = 1, c = 0;
for (int i = 2; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
printf("%lld", c);
}