码迷,mamicode.com
首页 > 其他好文 > 详细

高精度模板

时间:2018-03-10 11:51:46      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:algorithm   pac   +=   include   cstring   name   std   blog   bool   

 

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();
}

 

高精度模板

标签:algorithm   pac   +=   include   cstring   name   std   blog   bool   

原文地址:https://www.cnblogs.com/bljfy/p/8537296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!