求助!!!为什么n>=9时就不对?

P1255 数楼梯

pengqiushi @ 2022-10-07 12:05:41

代码如下:

#include<iostream>
using namespace std;
int a[105],la=1,b[105],lb=1,sum[105],lsum=1;
void cmp()
{
    //cout<<sum[1];
    la=lb;
    for(int i=1; i<=lb; i++)
        a[i]=b[i];
    lb=lsum;
    for(int i=1; i<=lsum; i++)
        b[i]=sum[i];
    int c=0;
    //lsum=1;
    for(int i=1; i<=1; i++){
        sum[i]=a[i]+b[i];
        if(sum[i]>=10){
            sum[i+1]++;
            sum[i]-=10;
            if(i==lsum){
                lsum++;
                //break;
            }
        }
    }
}
int main()
{
    int n;
    cin>>n;
    if(n==1||n==2){
        cout<<"1";
        return 0;
    }
    a[1]=1;
    b[1]=1;
    for(int i=1; i<=n; i++)
        cmp();
    /*if(n>=7)
        sum[lsum]++;*/
    for(int i=lsum; i>=1; i--)
        cout<<sum[i];
    return 0;
}

by _coastline_ @ 2022-10-07 22:01:31

@pengqiushi


by pengqiushi @ 2022-10-07 22:09:49

@xujiabin194 60分......


by _coastline_ @ 2022-10-07 22:10:39

@pengqiushi QAQ!!!


by _coastline_ @ 2022-10-07 22:11:10

@pengqiushi 发个测评结果网址我看一下咋了究竟


by pengqiushi @ 2022-10-07 22:13:02

@xujiabin194 https://www.luogu.com.cn/record/89096552


by _coastline_ @ 2022-10-07 22:14:52

这是我AC代码,你对照一下有没有什么遗漏的bug吧(本蒟蒻已经看不出来错误了QAQ)

#include<iostream>
#include<cmath>
using namespace std;
int n1[12345] , n2[12345] , n3[12345] , l1 = 1 , l2 = 1 , l3;
int main()
{
    int n;
    cin >> n;
    n1[1] = 1;n2[1] = 2;
    if(n == 1) cout << 1 << endl;
    else if(n == 2) cout << 2 << endl;
    else
    {
        for(int i = 3;i <= n;i++)
        {
            l3 = max(l1 , l2);
            for(int j = 1;j <= l3;j++)
            {
                n3[j] += n1[j] + n2[j];
                if(n3[j] >= 10)
                {
                    n3[j+1]++;
                    n3[j] %= 10;
                }
            }
            if(n3[l3+1]) l3++;
            l1 = l2;
            for(int j = 1;j <= l1;j++) n1[j] = n2[j];
            l2 = l3;
            for(int j = 1;j <= l2;j++)
            {
                n2[j] = n3[j];
                n3[j] = 0;
            }
        }
        for(int i = l2;i;i--) cout << n2[i];
        cout << endl;
    }
    return 0;
}

by pengqiushi @ 2022-10-07 22:33:28

@xujiabin194 非常谢谢,终于AC了QWQ!!!


by _coastline_ @ 2022-10-08 17:27:34

@pengqiushi 不用谢QWQ


上一页 |