Files
python/TangDou/LuoGuBook/P1255.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

50 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
/**
* 功能:高精度加法模板
* @param A
* @param B
* @return
*/
vector<int> add(vector<int> &A, vector<int> &B) {
if (A.size() < B.size()) return add(B, A);
int t = 0;
vector<int> C;
for (int i = 0; i < A.size(); i++) {
t += A[i];
if (i < B.size()) t += B[i];
C.push_back(t % 10);
t /= 10;
}
if (t) C.push_back(t);
return C;
}
vector<int> A, B, C;
int main() {
int n;
cin >> n;
if (n <= 2) {
printf("%d", n);
return 0;
}
//初始化一阶台阶步数
A.push_back(1);
//初始化二阶台阶步数
B.push_back(2);
//以后每阶台阶,都是可以从i-1,i-2阶台阶迁移过来,方法数就是两者的和
for (int i = 3; i <= n; i++) {
C = add(A, B);
//A<---B 方便下次计算
A.assign(B.begin(), B.end());
//B<---C 方便下次计算
B.assign(C.begin(), C.end());
}
//倒序输出结果
for (int i = B.size() - 1; i >= 0; i--)
printf("%d", B[i]);
return 0;
}