「模板」高精度

目前缺少高精减低精

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e5;

struct INT
{
    int a[N];

    INT()
    {
        memset(a, 0, sizeof(a));
    }

    void rd()
    {
        char s[N];
        scanf("%s", s);
        a[0] = strlen(s);
        for(int i = 1; i <= a[0]; i++)
            a[i] = s[a[0] - i] - '0';
        return;
    }

    void print()
    {
        for(int i = a[0]; i >= 1; i--)
            cout << a[i];
        cout << endl;
        return;
    }

    INT operator + (const INT &b) const
    {
        INT c;
        c.a[0] = max(a[0], b.a[0]);
        for(int i = 1; i <= c.a[0]; i++)
        {
            c.a[i] += a[i] + b.a[i];
            c.a[i + 1] += c.a[i] / 10;
            c.a[i] %= 10;
        }
        if(c.a[c.a[0] + 1]) c.a[0]++;
        return c;
    }

    INT operator + (const int &b) const
    {
        INT c;
        memcpy(c.a, a, sizeof(a));
        c.a[1] += b;
        for(int i = 1; i <= c.a[0]; i++)
            if(c.a[i] >= 10) c.a[i + 1] += c.a[i] / 10, c.a[i] %= 10;
            else break;
        if(c.a[c.a[0] + 1]) c.a[0]++;
        return c;
    }

    bool operator < (const INT &b) const
    {
        if(a[0] != b.a[0]) return a[0] < b.a[0];
        for(int i = a[0]; i >= 1; i--)
            if(a[i] != b.a[i]) return a[i] < b.a[i];
        return false;
    }

    INT operator - (INT b) const
    {
        INT t;
        memcpy(t.a, a, sizeof(a));
        if(t < b)
        {
            printf("-");
            swap(t, b);
        }
        for(int i = 1; i <= t.a[0]; i++)
        {
            if(t.a[i] < b.a[i]) t.a[i] += 10,  t.a[i + 1]--;
            t.a[i] -= b.a[i];
        }
        while(t.a[0] > 1 && !t.a[t.a[0]]) t.a[0]--;
        return t;
    }

    INT operator * (const INT &b) const
    {
        INT c;
        for(int i = 1; i <= a[0]; i++)
        {
            int w = 0;
            for(int j = 1; j <= b.a[0]; j++)
            {
                c.a[i + j - 1] += a[i] * b.a[j] + w;
                w = c.a[i + j - 1] / 10;
                c.a[i + j - 1] %= 10;
            }
            c.a[i + b.a[0]] += w;
        }
        c.a[0] = a[0] + b.a[0];
        if(!c.a[c.a[0]]) c.a[0]--;
        return c;
    }

    INT operator * (const int &b) const
    {
        INT c;
        c.a[0] = a[0];
        for(int i = 1; i <= a[0]; i++)
        {
            c.a[i] += a[i] * b;
            c.a[i + 1] += c.a[i] / 10;
            c.a[i] %= 10;
        }
        int len = c.a[c.a[0] + 1] ? c.a[0] + 1 : c.a[0];
        while(c.a[len] >= 10) c.a[len + 1] = c.a[len] / 10, c.a[len] %= 10, len++;
        c.a[0] = len;
        return c;
    }

    INT operator / (const int &b) const
    {
        INT c;
        memcpy(c.a, a, sizeof(a));
        for(int i = a[0]; i > 1; i--)
        {
            c.a[i - 1] += (c.a[i] % b) * 10;
            c.a[i] /= b;
        }
        c.a[1] /= b;
        while(!c.a[c.a[0]]) c.a[0]--;
        return c;
    }

    void operator = (int b)
    {
        while(b) a[++a[0]] = b % 10, b /= 10;
    }
} a, b, c;

int main()
{
    a.rd();
    b.rd();
    // c = a + b;
    // c = a - b;
    // c = a * b;
    c.print();
    return 0;
}

压位(4)

struct INT
{
    short a[20000];

    INT() {memset(a, 0, sizeof(a));}

    void read()
    {
        memset(a, 0, sizeof(a));
        string s; cin >> s;
        int len = s.size();
        for(int i = 1; i <= len; i += 4, a[0]++)
            for(int j = i, pw = 1; j <= min(i + 3, len); j++, pw *= 10)
                a[a[0] + 1] += (s[j - 1] - '0') * pw;
        return;
    }

    void print()
    {
        cout << a[a[0]];
        for(int i = a[0] - 1; i >= 1; i--)
        {
            int tmp = a[i], cnt = 0;
            while(tmp) tmp /= 10, cnt++;
            while(cnt < 4) cout << '0', cnt++;
            if(a[i]) cout << a[i];
        }
        cout << '\n';
        return;
    }

    void operator = (int b)
    {
        while(b) a[++a[0]] = b % 10000, b /= 10000;
    }

    bool operator < (const INT &b) const
    {
        if(a[0] != b.a[0]) return a[0] < b.a[0];
        for(int i = a[0]; i >= 1; i--)
            if(a[i] != b.a[i]) return a[i] < b.a[i];
        return false;
    }

    INT operator * (const ll &b) const
    {
        INT c;
        ll tmp[20000]; memset(tmp, 0, sizeof(tmp));
        tmp[0] = a[0];
        for(int i = 1; i <= a[0]; i++)
        {
            tmp[i] += a[i] * b;
            tmp[i + 1] += tmp[i] / 10000;
            tmp[i] %= 10000;
        }
        int len = tmp[tmp[0] + 1] ? tmp[0] + 1 : tmp[0];
        while(tmp[len] >= 10000) tmp[len + 1] = tmp[len] / 10000, tmp[len] %= 10000, len++;
        tmp[0] = len;
        for(int i = 0; i <= tmp[0]; i++) c.a[i] = tmp[i];
        return c;
    }
};
posted @ 2021-07-22 21:50  Acestar  阅读(67)  评论(0编辑  收藏  举报