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

大数加法、大数乘法

时间:2018-09-15 13:50:32      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:stream   printf   clu   ble   大数   vector   isp   void   进制   

大数加法

hdu1002

 

技术分享图片
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
const int MOD = 1e9 + 7;

#define MemI(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

void Add(char a[MAXN], char b[MAXN], int alen, int blen)
{
    int len = max(alen, blen);
    int aa[MAXN] = {0}, bb[MAXN] = {0};
    for(int i = 0;i < alen;++i)
        aa[i] = a[alen - i - 1] - 0;
    for(int i = 0;i < blen;++i)
        bb[i] = b[blen - i - 1] - 0;
    int ans[MAXN];
    Mem0(ans);
    for(int i = 0;i < len;++i)
        ans[i] = aa[i] + bb[i];
    for(int i = 0;i < len;++i)
    {
        if(ans[i] > 9)
        {
            ans[i] -= 10;
            ans[i + 1]++;
        }
    }
    if(ans[len])
        len++;
    bool flag = false;
    for(int i = len - 1;i >= 0;--i)
    {
        if(ans[i] != 0)
            flag = true;
        if(flag)
            cout << ans[i];
    }
    if(!flag)
        cout << 0;
    cout << endl;
}

int main()
{
    int T;
    while(cin >> T)
    {
        for(int cas = 1;cas <= T;++cas)
        {
            char a[MAXN], b[MAXN];
            cin >> a >> b;
            if(cas != 1)
                cout << endl;
            cout << "Case " << cas << ":\n" << a << " + " << b << " = ";
            Add(a, b, strlen(a), strlen(b));
        }
    }
    return 0;
}
View Code

 

大数乘法

51nod 1027

 

技术分享图片
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1005;
const int MOD = 1e9 + 7;

#define MemI(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

char a[MAXN], b[MAXN];
int ans[MAXN * MAXN] = {0};
int main()
{
    cin >> a >> b;
    int alen = strlen(a), blen = strlen(b);
    for(int i = 0;i < alen >> 1;++i)
        swap(a[i], a[alen - i - 1]);
    for(int i = 0;i < blen >> 1;++i)
        swap(b[i], b[blen - i - 1]);
    for(int i = 0;i < alen;++i)
    {
        for(int j = 0;j < blen;++j)
        {
            ans[i + j] += (a[i] - 0) * (b[j] - 0);
            ans[i + j + 1] += ans[i + j] / 10;
            ans[i + j] %= 10;
        }
    }
    bool flag = false;
    for(int i = alen + blen - 1;i >= 0;--i)
    {
        if(!flag && ans[i] != 0)
            flag = true;
        if(flag)
            cout << ans[i];
    }
    if(!flag)
        cout << 0;
    cout << endl;
    return 0;
}
View Code

 hdu1042

 

技术分享图片
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <set>
#include <map>
#include <vector>
#include <queue>
#include <iomanip>
#include <stack>

using namespace std;

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100005;
const int MOD = 1e9 + 7;

#define MemI(x) memset(x, -1, sizeof(x))
#define Mem0(x) memset(x, 0, sizeof(x))
#define MemM(x) memset(x, 0x3f, sizeof(x))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

int main()
{
    int n, a[MAXN];
    while(cin >> n)
    {
        Mem0(a);
        int len = 0, t = 0;
        a[0] = 1;
        //万进制
        for(int i = 1;i <= n;++i)
        {
            for(int j = 0;j <= len;++j)
            {
                a[j] = a[j] * i + t;
                t = a[j] / 10000;
                a[j] %= 10000;
            }
            if(t)
            {
                a[++len] = t;
                t = 0;
            }
        }
        printf("%d", a[len]);
        for(int i = len - 1;i >= 0;--i)
            printf("%04d", a[i]);
        printf("\n");
    }
    return 0;
}
View Code

 

大数加法、大数乘法

标签:stream   printf   clu   ble   大数   vector   isp   void   进制   

原文地址:https://www.cnblogs.com/shuizhidao/p/9643164.html

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