怎么办办办!!!!!!!!!!!50分

P1255 数楼梯

makeway @ 2024-12-26 17:39:08

#include <bits/stdc++.h>
using namespace std;
unsigned long long int x;
long long int f(long long int x)
{
    if(x==0||x==1)return 1;
    else return f(x-1)+f(x-2);
}
int main()
{
    cin>>x;
    cout<<f(x);
    return 0;
}

by xyx404 @ 2024-12-26 17:48:56

@makeway

需要高精。


by 11514zbs @ 2024-12-26 19:04:39

P y t h o n


by swsy2345b @ 2024-12-28 09:50:12

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
string s[5010];
long long a[N],b[N],c[N],n;
string add(string str,string str1)
{
    for (int i = 0, j = str.size() - 1; i < str.size(); i++, j--) {
        a[j]=str[i]-'0';
    }
    for (int i = 0, j = str1.size() - 1; i < str1.size(); i++, j--) {
        b[j]=str1[i]-'0';
    }
    int n = max(str.size(), str1.size());
    for (int i = 0; i < n; i++) {
        c[i] += a[i] + b[i];
        if (c[i] >= 10) {
            c[i + 1] = 1;
            c[i] -= 10;
        }
        if (i == n - 1 && c[n]) n++;//高精度加法
    }
    string str2;
    for (int i = n - 1; i >= 0; i--) {
        str2+=to_string(c[i]);
    }
    for(int i=0;i<n;i++){
        a[i]=b[i]=c[i]=0;
    }
    return str2;
}
int main()
{
    s[1]="1";
    s[2]="2";
    cin >> n;
    for(int i=3;i<=n;i++)
    {
        s[i]=add(s[i-1],s[i-2]);
    }
    cout << s[n];
    return 0;
} 

|