20分求助!

P1255 数楼梯

lijuncheng_1207 @ 2023-07-11 22:44:06

#include<bits/stdc++.h>
using namespace std;
vector<int> addition(vector<int> &a,vector<int> &b){
    vector<int> c;
    int la=a.size(),lb=b.size(),ans=0;
    if(la<lb)
        return addition(b,a);
    for(int i=0;i<=la;i++){
        ans+=a[i];
        if(i<lb) ans+=b[i];
        c.push_back(ans%10);
        ans/=10;
    }
    return c;
}
int main(){
    vector<int> a,b,c;
    int n;
    cin>>n;
    a.push_back(1);
    b.push_back(2); 
    if(n<=2){
        cout<<n;
        return 0;
    }
    for(int i = 3; i <= n; i++){
        c=addition(a,b);
        a=b;
        b=c;
    }
   int cl=c.size(),pos;
   for(int i=cl-1;i>=0;i--){
      if(c[i]!=0){
         pos=i;
         break;
      }
   }
   for(int i=pos;i>=0;i--)
      cout<<c[i];
    return 0;
}

|