lucy2012 @ 2024-04-05 21:56:07
#include<bits/stdc++.h>
using namespace std;
int n;
string fib[60],a[60];
int main(){
cin>>n;
fib[1]="1";
fib[2]="2";
for(int i=3;i<=n;i++){
int num[10000],cnt=0;
for(int j=fib[n-1].length();j>=0;j--){
num[cnt]=fib[n-1][j]-'0'+fib[n-2][j]-'0';
num[cnt+1]=num[cnt]/10;
num[cnt]%=10;
fib[i][j]=num[cnt++]+'0';
}
if(fib[i][0]=='0') {
for(int j=0;j<fib[i].length()-1;j++)
fib[i][j]=fib[i][j+1];
}
}
for(int i=0;i<fib[n].length();i++)
cout<<fib[n][i];
return 0;
}
by fight_for_humanity @ 2024-04-06 08:24:28
建议平时写高精度的时候直接封装成一个函数,便于调用。举例:
struct NUM{
int c[210];
int& operator [](const int i){ return c[i]; }
NUM(){ memset(c,0,sizeof c); c[0] = 1; }
void init(){ c[0] = c[1] = 1; }
NUM friend operator +(NUM x,NUM y){
NUM res;
res[0] = max(x[0],y[0]);
for(int i = 1;i<=res[0]+1;++i){
res[i] += x[i] + y[i];
res[i+1] += res[i] / 10;
res[i] %= 10;
}
for(int i = 200;i;--i)
if(res[i]){ res[0] = i; break; }
return res;
}
void output(){
for(int i = c[0];i;--i)printf("%d",c[i]);
}
}f[N];
tips:我这里自己写的,c[0]的含义是数的位数len。
by fight_for_humanity @ 2024-04-06 08:26:26
哦,说错了是封装为结构体
by fight_for_humanity @ 2024-04-06 08:36:48
@lucy2012 主函数
scanf("%d",&n);
f[1].init(),f[0].init();
for(int i = 2;i<=n;++i)f[i] = f[i-1] + f[i-2];
f[n].output();
还有这道题结构体里c要开大点
by I_Love_DS @ 2024-04-06 08:40:50
@fight_for_humanity 个人觉得函数更好