题解
2024-06-22 11:13:32
发布于:广东
54阅读
0回复
0点赞
vector<int> add(const vector<int>& a, const vector<int>& b) {
vector<int> res(max(a.size(), b.size()) + 1, 0);
int carry = 0;
for (size_t i = 0; i < res.size(); ++i) {
if (i < a.size()) res[i] += a[i];
if (i < b.size()) res[i] += b[i];
res[i] += carry;
carry = res[i] / 10;
res[i] %= 10;
}
while (res.size() > 1 && res.back() == 0) res.pop_back();
return res;
}
void print(const vector<int>& num) {
for (auto it = num.rbegin(); it != num.rend(); ++it) {
cout << *it;
}
cout << endl;
}
int main() {
int N;
cin >> N;
vector<vector<int>> dp(N + 1, vector<int>(1, 0));
dp[0][0] = 1;
dp[1][0] = 1;
for (int i = 2; i <= N; ++i) {
dp[i] = add(dp[i - 1], dp[i - 2]);
}
print(dp[N]);
return 0;
}
全部评论 1
潮水你快通过我微信!!!
2024-08-16 来自 浙江
0
有帮助,赞一个