80分求救

P1255 数楼梯

HarrycodingH @ 2024-08-23 11:15:46


#include <bits/stdc++.h>
using namespace std;
int a[5005],b[5005],c[5005],n,lc=1;
int main(){
    cin >> n;
    a[1]=1,b[1]=2;
    for (int i=3;i<=n;i++){
        memset(c,0,sizeof(c));
        for(int j=1;j<=lc;j++){
            c[j]+=a[j]+b[j];
            c[j+1]+=c[j]/10;
            c[j]%=10;
        }
        if(c[lc+1]>0) lc++;
        // c=a+b
        for (int j=1;j<=lc;j++){
            a[j]=b[j];
        }
        //a=b
        for (int j=1;j<=lc;j++){
            b[j]=c[j];
        }
      //b=a;
    }
    for (int i=lc;i>=1;i--) cout << c[i];
    return 0;
}

by wsljh @ 2024-08-23 11:34:27

@HarrycodingH 你for从3开始,所以要特判n==1||n==2的情况


by wsljh @ 2024-08-23 11:34:47

#include <bits/stdc++.h>
using namespace std;
int a[5005],b[5005],c[5005],n,lc=1;
int main(){
    cin >> n;
    if(n==1)
    {
        cout<<1;
        return 0;
    }   
    else if(n==2)
    {
        cout<<2;
        return 0;
    }
    a[1]=1,b[1]=2;
    for (int i=3;i<=n;i++){
        memset(c,0,sizeof(c));
        for(int j=1;j<=lc;j++)
        {
            c[j]+=a[j]+b[j];
            c[j+1]+=c[j]/10;   
            if(j==lc&&c[j]/10)
                lc++;
            c[j]%=10;
        }
        // c=a+b
        for (int j=1;j<=lc;j++){
            a[j]=b[j];
        }
        //a=b
        for (int j=1;j<=lc;j++){
            b[j]=c[j];
        }
      //b=c;
    }
    for (int i=lc;i>=1;i--) cout << c[i];
    return 0;
}

by wsljh @ 2024-08-23 11:35:07

像这样


by HarrycodingH @ 2024-08-23 11:59:54

@wsljh 谢谢 已关 %%%


|