救救我吧,一直50分

P1255 数楼梯

joshua_song @ 2022-03-13 10:42:41

源代码:

#include<bits/stdc++.h>
using namespace std;
int fib(int x)
{
    vector <int> f;
    f.push_back(0);
    f.push_back(1);
    f.push_back(2);
    if(x==1||x==2)
        return x;
    for(int i=3;i<=x+1;i++)
        f.push_back(f[i-2]+f[i-1]); 
    return f[x];
}
int main()
{
    int x;
    cin>>x;
    cout<<fib(x)<<endl;
    return 0;
}
评测记录链接

by strange757 @ 2022-03-13 10:46:57

这题要写高精度


by joshua_song @ 2022-03-13 10:48:07

语言:C++


by joshua_song @ 2022-03-13 10:48:35

@strange757 ok


by joshua_song @ 2022-03-13 10:53:23

@strange757

#语言:python
def fib(value):
   f=[0,1,2]
   if value==1 or value==2:
    return value
   for i in range(3,value+1):
    f.append(f[i-2]+f[i-1]) 
   return f[value]

a=int(input())
print(fib(a))

不过PYTHON像不用高精度


by tommyfj @ 2022-03-13 10:57:07

@joshua_song

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

by joshua_song @ 2022-03-13 10:59:18

换成Long long以后白嫖十分

#include<bits/stdc++.h>
using namespace std;
long long fib(int x)
{
    vector <long long> f;
    f.push_back(0);
    f.push_back(1);
    f.push_back(2);
    if(x==1||x==2)
        return x;
    for(long long i=3;i<=x+1;i++)
        f.push_back(f[i-2]+f[i-1]); 
    return f[x];
}
int main()
{
    int x;
    cin>>x;
    cout<<fib(x)<<endl;
    return 0;
}

链接


by zyx15801617101 @ 2022-03-13 11:01:14

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int n,len=1,f[5003][5003];
void hp(int k)
{    
    int i;
    for(i=1;i<=len;i++)
     f[k][i]=f[k-1][i]+f[k-2][i];
    for(i=1;i<=len;i++)
     if(f[k][i]>=10)
     {
         f[k][i+1]+=f[k][i]/10;
         f[k][i]=f[k][i]%10;
         if(f[k][len+1])len++;
    }
}
int main()
{
    int i;
    scanf("%d",&n);
    f[1][1]=1; f[2][1]=2;       
    for(i=3;i<=n;i++) 
     hp(i);                         
    for(i=len;i>=1;i--)
     printf("%d",f[n][i]);
    return 0;
}

by joshua_song @ 2022-03-13 11:02:17

@tommyfj 谢谢


by joshua_song @ 2022-03-13 11:02:51

@zyx15801617101 谢谢


by tommyfj @ 2022-03-13 11:02:56

不用谢


| 下一页