然而会崩溃

P1255 数楼梯

a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 15:09:08

rt,此代码会崩溃。错误的地方我已经找出来了,求调%%%悬赏关注

#include<bits/stdc++.h>
using namespace std;
#define maxn 100000
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch=='-') {
            f=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
inline void write(int x) {
    if(x<0)
        putchar('-'),x=-x;
    if(x>9) {
        write(x/10);
    }
    putchar(x%10 +'0');
}
struct bigint {
    int len,a[maxn];
    inline bigint(int x=0) {
        memset(a,0,sizeof(a));
        for(len=1; x; len++) {
            a[len]=x%10;
            x/=10;
        }
        len--;
    }
    int &operator [](int i) {
        return a[i];
    }
    inline void flatten(int l) {
        len=l;
        for(int i=1; i<len; i++)a[i+1]+=a[i]/10,a[i]%=10;
        while(!a[len])len--;
    }
    inline void putbig() {
        for(int i=max(len,1); i>=1; i--) {
            write(a[i]);
        }
    }
};

bigint operator+(bigint a,bigint b){
    bigint c;
    int len =max(a.len,b.len);
    for(int i=1;i<=len;i++){
        c[i]+=a[i]+b[i];
    }
    c.flatten(len+1);
    return c;
}
int main() {
    bigint l1=1;
    bigint l2=0;
    bigint nw=0;
    int b=read();
    bigint t; 
    for(int i=0; i<b; i++) {
        t=nw;
        nw=l1+l2;//error line!
        l2=l1;
        l1=t;
    }
    nw.putbig();
    return 0;
}

by jltxsfl @ 2022-07-19 16:12:01

你发出的第一份代码,倒数5-8行改一下就可ac


by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 16:12:05

本帖SPFA


上一页 |