请问为什么照着深基上打的代码却在重载运算符出了问题?谢谢。

P1009 [NOIP1998 普及组] 阶乘之和

tanghg @ 2022-06-03 08:12:46

rt。

#include <iostream>
#include <cstring>
using namespace std;
const long long MAX=100;
typedef int  ll;
struct Bigint{
    ll l,a[MAX];
    Bigint(ll x=0){
        memset(a,0, sizeof(a));
        for (l  = 1; x ; ++l) {
            a[l]=x%10;
            x/=10;
        }
        l--;
    }
    int &operator[](ll i){
        return a[i];
    }
    void fla(ll L){
        l=L;
        for (int i = 1; i <=l ; ++i) {
            a[i+1]=a[i]/10;
            a[i]%=10;
        }
        while (!a[l]){
            l--;
        }
    }
    void out(){
        for (int i = max(1,l); i >=1 ; --i) {
            cout<<a[i];
        }
    }
    Bigint operator+(Bigint a,Bigint b){
        Bigint c;
        ll l= max(a.l,b.l);
        for (int i = 1; i <=l ; ++i) {
            c[i]+=a[i]+b[i];
        }
        c.fla(l+1);
        return c;
    }
    Bigint operator*(Bigint a, ll b){
        Bigint c;
        ll l=a.l;
        for (int i = 1; i <=l ; ++i) {
            c[i]=a[i]*b;
        }
        c.fla(l+11);
        return c;
    }
};
int main() {
    Bigint a(0),f(1);
    ll n;
    cin>>n;
    for (int i = 1; i <=n ; ++i) {
        f= f * i;
        a= a + f;
    }
    a.out();
    return 0;
}

还有一个小问题。

Bigint(ll x=0){
        memset(a,0, sizeof(a));
        for (l  = 1; x ; ++l) {
            a[l]=x%10;
            x/=10;
        }
        l--;
    }

这一段代码有什么作用呢,为什么要这样初始化? 第一次发帖,有什么问题我会改。


by tanghg @ 2022-06-03 09:13:10

你把这段代码放到洛谷IDE里面就可以。

#include <iostream>
#include<cstring>
using namespace std;
const long long MAX=1000000;
typedef int  ll;
struct Bigint{
    ll l,a[MAX];
    Bigint(ll x=0){
        memset(a,0, sizeof(a));
        for (l  = 1; x ; ++l) {
            a[l]=x%10;
            x/=10;
        }
        l--;
    }
    int &operator[](ll i){
        return a[i];
    }
    void fla(ll L){
        l=L;
        for (int i = 1; i <=l ; ++i) {
            a[i+1]=a[i]/10;
            a[i]%=10;
        }
        while (!a[l]){
            l--;
        }
    }
    void out(){
        for (int i = max(1,l); i >=1 ; --i) {
            cout<<a[i];
        }
    }

};
Bigint operator+(Bigint a,Bigint b){
    Bigint c;
    ll l= max(a.l,b.l);
    for (int i = 1; i <=l ; ++i) {
        c[i]+=a[i]+b[i];
    }
    c.fla(l+1);
    return c;
}
Bigint operator*(Bigint a, ll b){
    Bigint c;
    ll l=a.l;
    for (int i = 1; i <=l ; ++i) {
        c[i]=a[i]*b;
    }
    c.fla(l+11);
    return c;
}
int main() {
    Bigint a(0),f(1);
    ll n;
    cin>>n;
    for (int i = 1; i <=n ; ++i) {
        f=f*i;
        a=a+f;
    }
    a.out();
    return 0;
}

by tanghg @ 2022-06-03 09:14:01

@6872 谢谢理解QWQ


by _6872_ @ 2022-06-03 09:24:28

@Tanghaoge kkk之前好像发过一个好像征集深基错误的帖,可以去找找反馈下AwAOwO


by tanghg @ 2022-06-03 09:25:51

@6872 应该不是深基错误,是我自己的错误


by Super_Supper @ 2022-06-03 09:39:44

@Tanghaoge 太阴间了,运算符重载应该有错,但是找不出来


by tanghg @ 2022-06-03 09:44:11

@sb_yyds oh好吧谢谢


by Super_Supper @ 2022-06-03 09:44:34

@Tanghaoge 在 fla 里面不应该写 =,应该写 +=


by Super_Supper @ 2022-06-03 09:45:51

@Tanghaoge

亲测 AC:

#include <iostream>
#include <cstring>
using namespace std;
const long long MAX=1000000;
typedef int  ll;
struct Bigint{
    ll l,a[MAX];
    Bigint(ll x=0){
        memset(a,0, sizeof(a));
        for (l  = 1; x ; ++l) {
            a[l]=x%10;
            x/=10;
        }
        l--;
    }
    int &operator[](ll i){
        return a[i];
    }
    void fla(ll L){
        l=L;
        for (int i = 1; i <=l ; ++i) {
            a[i+1]+=a[i]/10;
            a[i]%=10;
        }
        while (!a[l]){
            l--;
        }
    }
    void out(){
        for (int i = max(1,l); i >=1 ; --i) {
            cout<<a[i];
        }
    }

};
Bigint operator+(Bigint a,Bigint b){
    Bigint c;
    ll l= max(a.l,b.l);
    for (int i = 1; i <=l ; ++i) {
        c[i]+=a[i]+b[i];
    }
    c.fla(l+1);
    return c;
}
Bigint operator*(Bigint a, ll b){
    Bigint c;
    ll l=a.l;
    for (int i = 1; i <=l ; ++i) {
        c[i]=a[i]*b;
    }
    c.fla(l+11);
    return c;
}
int main() {
    Bigint a(0),f(1);
    ll n;
    cin>>n;
    for (int i = 1; i <=n ; ++i) {
        f=f*i;
        a=a+f;
    }
    a.out();
    return 0;
}

by tanghg @ 2022-06-03 10:34:52

@sb_yyds 谢谢


by tanghg @ 2022-06-03 10:38:28

@sb_yyds 我自己根本找不出来,谢谢大佬


上一页 | 下一页