大數加法乘法

阿 霖發表於2020-10-08
int* mul(int* buf, int k){
    ll t = 0;
    for(int i = 0; i < 50; i++){
        int s = buf[i]*k+t;
        buf[i] = s % 10;
        t = s / 10;
    }
    return buf;
}

void print(int* buf){
    for (int i = 60; i >= 0; i--) {
        if (buf[i] == 0) continue;
        for (int j = i; j >= 0; j--) {
            cout << buf[j];
        }
        break;
    }
}

int* add(int* buf, int k){
    buf[0] += k;
    int t = 0;
    for (int i = 0; i < 50; i++) {
        int s = buf[i]+t;
        buf[i] = s % 10;
        t = s / 10;
    }

    return buf;
}

int* add(int* buf1, int* buf2){
    for(int i = 60; i >= 0; i--){
        buf1[i] += buf2[i];
    }
    int t = 0;
    for (int i = 0; i < 50; i++) {
        int s = buf1[i]+t;
        buf1[i] = s % 10;
        t = s / 10;
    }
    
    return buf1;
}

相關文章