c++50分

P1255 数楼梯

Mark520 @ 2023-07-19 14:17:37

#include<bits/stdc++.h>
using namespace std;
int df(int a){
    if(a<2)return 1;
    else
    return df(a-1)+df(a-2);

}
int main(){
    int a;

    while(cin>>a)

    cout<<df(a)<<endl;

}

by siuuuuu @ 2023-07-19 14:22:20

@Mark520 这题要高精度的


by siuuuuu @ 2023-07-19 14:24:51

自己写的可以看看

#include<iostream>
#include<algorithm>
using namespace std;
int a[2000]={1},b[2000]={1},c[2000];
int lena=1,lenb=1,lenc,carry;
int main(){
    int n;
    cin>>n;
    if(n==1){
        cout<<1;
        return 0;
    }
    for(int i=2;i<=n;i++){
        lenc=max(lena,lenb);
        carry=0;
        for(int j=0;j<lenc;j++){
            int t=a[j]+b[j]+carry;
            carry=t/10;
            c[j]=t%10;
        }
        if(carry){
            c[lenc]=carry;
            lenc++;
        }
        for(int j=0;j<lenb;j++)
            a[j]=b[j];
        lena=lenb;
        for(int j=0;j<lenc;j++)
            b[j]=c[j];
        lenb=lenc;
    }
    for(int i=lenc-1;i>=0;i--)
        cout<<c[i]; 
    return 0;
}

by codejiahui @ 2023-07-19 14:32:21

@Mark520 自己的

#include<iostream>
using namespace std;
int f[5010][2010];
void printBIG(int* a)
{
    for (int i = a[0];i >= 1;i--)
        cout << a[i];
    return;
}
void addBIG(int* x,int* y,int* z)
{
    z[0] = max(x[0],y[0]);
    for (int i = 1;i <= z[0];i++)
        z[i] = x[i] + y[i];
    for (int i = 1;i <= z[0];i++)
    {
        z[i + 1] += z[i] / 10;
        z[i] %= 10;
        if (z[z[0] + 1] > 0) z[0]++;
    }
}
int main()
{
    int n;
    cin >> n;
    f[1][0] = f[1][1] = 1;
    f[2][0] = 1;
    f[2][1] = 2;
    for (int i = 3;i <= n;i++)
        addBIG(f[i - 1],f[i - 2],f[i]);
    printBIG(f[n]);
    return 0;
}

|