90分,大佬帮忙看看

P1255 数楼梯

Zzy20060323 @ 2024-09-15 12:49:08

#include<stdio.h>
int a[5005][1005];
int main()
{
    int n;
    int m = 0;
    scanf("%d", &n);
    a[0][1] = 0;
    a[1][1] = 1;
    a[2][1] = 2;
    for (int i = 3; i <= n; i++)
    {
        for (int j = 1; j < 1003; j++)
        {
            a[i][j] += a[i - 1][j] + a[i - 2][j];
            if (a[i][j] > 9)
            {
                a[i][j + 1]++;
                a[i][j] %= 10;

            }
        }

    }

        for (int j = 1003; j >= 1; j--)
        {
            if (a[n][j] == 0 && !m)
            {
                continue;
            }
            m = 1;
            printf("%d", a[n][j]);
        }

    return 0;
}

by qiaochu @ 2024-09-15 13:07:54

@Zzy20060323

#include<bits/stdc++.h>
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 MinLand @ 2024-09-15 13:19:57

你这个高精度开小了。

最后一个测试点有1045个。


|