求助,20分,本地结果正确,lg不行

P1255 数楼梯

pshong @ 2023-10-31 17:12:32

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int n;
vector<int> res;
vector<int> add(vector<int>&a,vector<int>& b)
{
    if(a.size() < b.size()) return add(b,a);
    vector<int> c;
    int t;
    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); 
    while(c.size() && c.back() == 0) c.pop_back();
    return c;
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    cin>>n;
    if(n < 3)
    {
        cout<<n;
        return 0;
    }

    vector<int> t1,t2;
    t1.push_back(1);t2.push_back(2);
    for(int i = 3;i <= n;i ++)
    {
        res = add(t1,t2);
        t1.assign(t2.begin(),t2.end());
        t2.assign(res.begin(),res.end());
    }
    for(int i = res.size()-1;i >= 0;i --) cout<<res[i];

    return 0;
}

by LJC13618059782 @ 2023-10-31 20:39:01


#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> add(vector<int> &A, vector<int> &B)
{
    if (A.size() < B.size()) return add(B, A);
    vector<int> C;
    int t = 0;
    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;
}
int main(){
    while(cin>>n){
        vector<int> a={1},b={2},c;
        if(n==1){
            cout<<1<<endl;
        }
        if(n==2){
            cout<<2<<endl;
        }
        if(n>=3){
            for(int i=3;i<=n;i++){
                c=add(a,b);
                a=b;
                b=c;
            }
            for (int i = c.size() - 1; i >= 0; i--)
            cout<<c[i];
        cout<<endl;
        }      
    }
    return 0;

}

|