brandish @ 2024-06-16 21:04:31
#include<cstdio>
#include<vector>
using namespace std;
void fun(vector<int>& a, vector<int>& b) {
vector<int> temp(b.begin(), b.end());
temp.push_back(0);
for (int i = 0; i < a.size(); i++) {
temp[i] += a[i];
}
for (int i = 1; i < temp.size(); i++) {
temp[i] += temp[i - 1] / 10;
temp[i - 1] %= 10;
}
a.swap(b);
b.swap(temp);
}
int main()
{
int n;
scanf("%d",&n);
vector<int> a, b;
a.push_back(1);
b.push_back(1);
for (int i = 2; i <= n; i++) {
fun(a, b);
}
int index = b.size() - 1;
while (index >= 0) {
if (b[index] != 0) break;
index--;
}
for (int i = index; i >= 0; i--) {
printf("%d", b[index]);
}
return 0;
}
by 123huchenghao @ 2024-06-29 18:11:31
#include<iostream>
#include<cstring>
using namespace std;
char f[5005][2000];
inline void bigAdd(char *ret,const char *f1,const char *f2){
int flag = 0;
int i;
for(i = 0;f1[i]!='\0'||f2[i]!='\0'||flag;++i){
int a = f1[i]?f1[i]-'0':0;
int b = f2[i]?f2[i]-'0':0;
if(a+b+flag >= 10){
ret[i] = a+b+flag-10+'0';
flag = 1;
}
else{
ret[i] = a+b+flag+'0';
flag = 0;
}
}
}
int main(){
int n;
cin >> n;
strcpy(f[0],"0");
strcpy(f[1],"1");
strcpy(f[2],"2");
for(int i = 3;i <= n;i++)
bigAdd(f[i],f[i-2],f[i-1]);
for(int i = strlen(f[n])-1;i >= 0;--i)
cout << f[n][i];
cout << endl;
return 0;
}