高精度模板

 

A+B

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 2000

using namespace std;

int a[MAXN], b[MAXN];
char c[MAXN], d[MAXN];
int ans[MAXN];
int lans = 1, la, lb;

void High_precision_add() {
    int x = 0;
    while(lans <= la||lans <= lb) {
        ans[lans] = a[lans]+b[lans]+x;
        x = ans[lans]/10;
        ans[lans] = ans[lans]%10;
        lans++;
    }
    ans[lans] = x;
    while(ans[lans] == 0&&lans > 1) {
        lans--;
    }
    reverse(ans+1, ans+lans+1);
    for(int i=1; i<=lans; i++) {
        printf("%d", ans[i]);
    }
    printf("\n");
}

void read() {
    scanf("%s%s", c, d);
    la = strlen(c), lb = strlen(d);
    for(int i=0; i<la; i++) {
        a[la-i] = c[i]-'0';
    }
    for(int i=0; i<lb; i++) {
        b[lb-i] = d[i]-'0';
    }
}

int main() {
    read();
    High_precision_add();
}

A-B

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 10005

using namespace std;

char c[MAXN], d[MAXN];
int a[MAXN], b[MAXN];
int ans[MAXN];
int la, lb, lans = 1;

bool cmp() {
    if(la == lb) return c[la-1] >= d[lb-1];
    return la >= lb;
}

void read() {
    scanf("%s%s", c, d);
    la = strlen(c), lb = strlen(d);
    if(!cmp()) {
        printf("-");
        swap(la, lb);
        for(int i=0; i<la; i++) {
            a[la-i] = d[i]-'0';
        }
        for(int i=0; i<lb; i++) {
            b[lb-i] = c[i]-'0';
        }
    }
    else {
        for(int i=0; i<la; i++) {
            a[la-i] = c[i]-'0';
        }
        for(int i=0; i<lb; i++) {
            b[lb-i] = d[i]-'0';
        }
    }
}

void High_precision_subtraction() {
    int x = 0;
    while(lans <= la||lans <= lb) {
        if(a[lans]-b[lans]-x < 0) {
            ans[lans] = a[lans]+10-b[lans]-x;
            x = 1;
        }
        else {
            ans[lans] = a[lans]-b[lans]-x;
            x = 0;
        }
        lans++;
    }
    while(ans[lans] == 0&&lans > 1) {
        lans--;
    }
    reverse(ans+1, ans+lans+1);
    for(int i=1; i<=lans; i++) {
        printf("%d", ans[i]);
    }
    printf("\n");
}

int main() {
    read();
    High_precision_subtraction();
}

A*B

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 10005

using namespace std;

int la, lb;
char c[MAXN], d[MAXN];
int a[MAXN], b[MAXN];
int ans[MAXN];

void read() {
    scanf("%s%s", c, d);
    la = strlen(c), lb = strlen(d);
    for(int i=0; i<la; i++) {
        a[la-i] = c[i]-'0';
    }
    for(int i=0; i<lb; i++) {
        b[lb-i] = d[i]-'0';
    }
}

void High_precision_multiplication() {
    for(int i=1; i<=la; i++) {
        int x = 0;
        for(int j=1; j<=lb; j++) {
            ans[i+j-1] += x+a[i]*b[j];
            x = ans[i+j-1]/10;
            ans[i+j-1] %= 10;
        }
        ans[i+lb] = x;
    }
    int l = la+lb;
    while(ans[l] == 0&&l > 1) {
        l--;
    }
    reverse(ans+1, ans+l+1);
    for(int i=1; i<=l; i++) {
        printf("%d", ans[i]);
    }
    printf("\n");
}

int main() {
    read();
    High_precision_multiplication();
}

A/B

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 10005

using namespace std;

int la, lans = 1;
char c[MAXN];
int a[MAXN], b;
int ans[MAXN];

void read() {
    cin>>c;
    scanf("%d", &b);
    la = strlen(c);
    for(int i=0; i<la; i++) {
        a[i+1] = c[i]-'0';
    }
}

void High_precision_division() {
    int x = 0;
    for(int i=1; i<=la; i++) {
        ans[i] = a[i]/b;
        x = a[i]%b;
        a[i+1] += x*10;
    }
    while(lans < la&&ans[lans] == 0) {
        lans++;
    }
    for(int i=lans; i<=la; i++) {
        printf("%d", ans[i]);
    }
    printf("\n");
}

int main() {
    read();
    High_precision_division();
}

 

作者:wlz
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2018-03-10 08:09  Mystical-W  阅读(150)  评论(0编辑  收藏  举报