然而会崩溃

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 Shunpower @ 2022-07-19 15:10:50

你重载了加号吗?


by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 15:12:22

@Zealous_YH 重载了啊


by Shunpower @ 2022-07-19 15:15:59

@S__B

噢我眼瞎

你为什么要 len =max(a.len,b.len); 啊,应该是 min


by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 15:18:33

@Zealous_YH soga


by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 15:19:34

@Zealous_YH 还是不行


by Shunpower @ 2022-07-19 15:50:54

@S__B

那我不知道了/qd


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

@jltxsfl 好的,我试试


by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 16:06:55

@jltxsfl 还是崩了……似乎是加号的问题,可是找不出来


by jltxsfl @ 2022-07-19 16:10:42

#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++) {
        nw=l1+l2;
        l2=l1;
        l1=nw;
    }
    nw.putbig();
    return 0;
}

by a2lyaXNhbWUgbWFyaXNh @ 2022-07-19 16:11:44

似乎是DevCpp的问题,终


| 下一页