uva 424 Integer Inquiry(高精度加法模板) uva 10106 Product(高精度乘法模板)

uva 424 高精度加法。


代码1:

#include <stdio.h>
#include <string.h>
const int Maxn = 110;

char num[Maxn];
int sum[Maxn];

void add()
{
    int len = strlen(num);
    for (int i = 0; i < len; i++)
    {
        sum[i] += num[len - i - 1] - '0';
        if (sum[i] > 9)
        {
            sum[i] = sum[i] - 10;
            sum[i + 1] += 1;
        }
    }
}

int main()
{
    while (scanf("%s", num) != EOF)
    {
        getchar();
        if (num[0] == '0')
        {
            int i = Maxn;
            while (!sum[i])
                i--;
            for (; i >= 0; i--)
                printf("%d", sum[i]);
            printf("\n");
            break;
        }
        else
        {
            add();
        }
    }
    return 0;
}

代码2:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int Maxn = 1000;

struct BigInt
{
    int len, s[Maxn];

    //无参构造函数
    BigInt()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    //参数为数字类型的构造函数
    BigInt(int num)
    {
        *this = num;
    }
    //参数为字符数组类型的构造函数
    BigInt(const char* num)
    {
        *this = num;
    }

    //重载运算符'='用于赋值字符数组
    BigInt operator = (const char* num)
    {
        len = strlen(num);
        for (int i = 0; i < len; i++)
            s[i] = num[len - i - 1] - '0';
        return *this;
    }
    //重载运算符'='用于赋值数字
    BigInt operator = (int num)
    {
        char s[Maxn];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    //将字符数组转化成字符串
    string str() const
    {
        string res = "";
        for (int i = 0; i < len; i++)
        {
            res = (char)(s[i] + '0') + res;
        }
        if (res == "")
        {
            res = "0";
        }
        return res;
    }
    //'+' 运算符重载
    BigInt operator + (const BigInt& b) const
    {
        BigInt c;
        c.len = 0;
        for (int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if (i < len)
                x += s[i];
            if (i < b.len)
                x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    BigInt operator * (const BigInt& b) const
    {
        
    }
    //'+='运算符重载
    BigInt operator += (const BigInt& b)
    {
        *this = *this + b;
        return *this;
    }
    //'<'运算符重载
    bool operator < (const BigInt& b) const
    {
        if (len != b.len)
            return len < b.len;
        for (int i = len - 1; i >= 0; i++)
        {
            if (s[i] != b.s[i])
                return s[i] < b.s[i];
        }
        return false;
    }
    //'>'运算符重载
    bool operator > (const BigInt& b) const
    {
        return b < *this;
    }
    //'<='运算符重载
    bool operator <= (const BigInt& b) const
    {
        return !(b < *this);
    }
    //'>='运算符重载
    bool operator >= (const BigInt& b) const
    {
        return !(b > *this);
    }
    //'!='运算符重载
    bool operator != (const BigInt& b) const
    {
        return b < *this || b > *this;
    }
    //'=='运算符重载
    bool operator == (const BigInt& b) const
    {
        return !(b < *this) && !(b > *this);
    }
};

istream& operator >> (istream& in, BigInt& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream& out, const BigInt& x)
{
    out << x.str();
    return out;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    BigInt num = 0;
    BigInt ans = 0;
    while (cin >> num)
    {
        if (num == 0)
            break;
        ans += num;
    }
    cout << ans << endl;
    return 0;
}

uva 10106 高精度乘法。

代码1:

#include <stdio.h>
#include <string.h>
const int Maxn = 251;

char A[Maxn], B[Maxn];
int a[Maxn], b[Maxn];
int c[Maxn * Maxn];

int main()
{
    #ifdef LOCAL
    freopen("in.txt", "r", stdin);
    #endif // LOCAL
    while (scanf("%s%s", A, B) != EOF)
    {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        memset(c, 0, sizeof(c));
        int lena = strlen(A);
        int lenb = strlen(B);
        for (int i = 0; i < lena; i++)
        {
            a[i] = A[lena - i - 1] - '0';
        }
        for (int i = 0; i < lenb; i++)
        {
            b[i] = B[lenb - i - 1] - '0';
        }
        for (int i = 0; i < lena; i++)
        {
            for (int j = 0; j < lenb; j++)
            {
                c[i + j] = a[i] * b[j] + c[i + j];
            }
        }
        int jinwei = 0;
        int lenc = lena + lenb;
        for (int i = 0; i < lenc; i++)
        {
            int t = c[i] + jinwei;
            c[i] = t % 10;
            jinwei = t / 10;
        }
        while (c[lenc] == 0 && lenc > 0)//debug
            lenc--;
        for (int i = lenc; i >= 0; i--)
            printf("%d", c[i]);
        printf("\n");
    }
    return 0;
}

代码2:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int Maxn = 1000;

struct BigInt
{
    int len, s[Maxn];

    //无参构造函数
    BigInt()
    {
        memset(s, 0, sizeof(s));
        len = 1;
    }
    //参数为数字类型的构造函数
    BigInt(int num)
    {
        *this = num;
    }
    //参数为字符数组类型的构造函数
    BigInt(const char* num)
    {
        *this = num;
    }

    //重载运算符'='用于赋值字符数组
    BigInt operator = (const char* num)
    {
        len = strlen(num);
        for (int i = 0; i < len; i++)
            s[i] = num[len - 1 - i] - '0';
        return *this;
    }
    //重载运算符'='用于赋值数字
    BigInt operator = (int num)
    {
        char s[Maxn];
        sprintf(s, "%d", num);
        *this = s;
        return *this;
    }
    //将字符数组转化成字符串
    string str() const
    {
        string res = "";
        for (int i = 0; i < len; i++)
        {
            res = (char)(s[i] + '0') + res;
        }
        if (res == "")
        {
            res = "0";
        }
        return res;
    }
    //'+' 运算符重载
    BigInt operator + (const BigInt& b) const
    {
        BigInt c;
        c.len = 0;
        for (int i = 0, g = 0; g || i < max(len, b.len); i++)
        {
            int x = g;
            if (i < len)
                x += s[i];
            if (i < b.len)
                x += b.s[i];
            c.s[c.len++] = x % 10;
            g = x / 10;
        }
        return c;
    }
    //'*' 运算符重载
    BigInt operator * (const BigInt& b) const
    {
        BigInt c;
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < b.len; j++)
            {
                c.s[i + j] = s[i] * b.s[j] + c.s[i + j];
                //printf("----%d-----", c.s[i + j]);
            }
        }
        c.len = len + b.len;
        int jinwei = 0;
        for (int i = 0; i < c.len; i++)
        {
            int t = c.s[i] + jinwei;
            jinwei = t / 10;
            c.s[i] = t % 10;
        }
        while (c.s[c.len - 1] == 0 && c.len > 1)
            c.len--;
        return c;
    }
    //'+='运算符重载
    BigInt operator += (const BigInt& b)
    {
        *this = *this + b;
        return *this;
    }
    //'<'运算符重载
    bool operator < (const BigInt& b) const
    {
        if (len != b.len)
            return len < b.len;
        for (int i = len - 1; i >= 0; i++)
        {
            if (s[i] != b.s[i])
                return s[i] < b.s[i];
        }
        return false;
    }
    //'>'运算符重载
    bool operator > (const BigInt& b) const
    {
        return b < *this;
    }
    //'<='运算符重载
    bool operator <= (const BigInt& b) const
    {
        return !(b < *this);
    }
    //'>='运算符重载
    bool operator >= (const BigInt& b) const
    {
        return !(b > *this);
    }
    //'!='运算符重载
    bool operator != (const BigInt& b) const
    {
        return b < *this || b > *this;
    }
    //'=='运算符重载
    bool operator == (const BigInt& b) const
    {
        return !(b < *this) && !(b > *this);
    }
};

istream& operator >> (istream& in, BigInt& x)
{
    string s;
    in >> s;
    x = s.c_str();
    return in;
}

ostream& operator << (ostream& out, const BigInt& x)
{
    out << x.str();
    return out;
}

int main()
{
#ifdef LOCAL
    freopen("in.txt", "r", stdin);
#endif // LOCAL
    BigInt num1 = 0;
    BigInt num2 = 0;
    BigInt ans = 1;
    while (cin >> num1)
    {
        cin >> num2;
        ans = num2 * num1;
        cout << ans << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值