大数模板

 大数加法

string temp1,temp2,temp,str;
string sum(string s1,string s2)
{
    if(s1.length()<s2.length())
    {
        string temp=s1;
        s1=s2;
        s2=temp;
    }
    int i,j;
    for(i=s1.length()-1,j=s2.length()-1; i>=0; i--,j--)
    {
        s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));
        if(s1[i]-'0'>=10)
        {
            s1[i]=char((s1[i]-'0')%10+'0');
            if(i)
                s1[i-1]++;
            else
                s1='1'+s1;
        }
    }
    return s1;
}
string cmp(string aa,string bb)
{
    if(aa.size()>bb.size())
        return bb;
    if(aa.size()<bb.size())
        return aa;
    if(aa<bb)
        return aa;
    return bb;
}

  大数乘法

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#define N 30001
using namespace std ;
string mul(string a1,string b1)
{
    int lena = a1.size(), lenb =  b1.size(),x;
    int a[N],b[N],c[N];
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for ( int i = 0 ; i < lena ; i ++ )
        a [ lena - i ] = a1 [ i ] - 48 ;
    for ( int i = 0 ; i < lenb ; i ++ )
        b [ lenb - i ] = b1 [ i ] - 48 ;
    for ( int i = 0 ; i <= lena ; i ++ )
    {
        x = 0 ;  //用来存放进位
        for ( int j = 1 ; j <= lenb ; j ++ )
        {
            c [ i + j - 1 ] += a [ i ] * b [ j ] + x ;
            x = c [ i + j - 1 ] / 10 ;
            c [ i + j - 1 ] %= 10 ;
        }
        c [ i + lenb ] = x ;         //进位
    }
    int lenc = lena + lenb ;
    while ( c [ lenc ] == 0 && lenc > 1 )
        lenc -- ;
    string res;
    res.clear();
    for ( int i = lenc ; i > 0 ; i -- )
        res+=('0'+c[i]);
    return res;
}
int main()
{
    string x="1",y[55];
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<=9; j++)
        {
            if(i==0)
                y[i*10+j]='0'+j;
            else
            {
                y[i*10+j]='0'+i;
                y[i*10+j]+='0'+j;
            }
        }
    }
    y[50]="50";
    for(int i=2; i<=50; i++)
        x=mul(x,y[i]);
    cout<<x<<endl;
    return 0;
}

  

转载于:https://www.cnblogs.com/SDUTNING/p/11046148.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值