其他分享
首页 > 其他分享> > ICPC:大数的乘法

ICPC:大数的乘法

作者:互联网

题目描述:

求两个不超过100位的非负整数的乘积。

输入:

有两行,每行是一个不超过100位的非负整数,没有多余的前导0。

输出:

 一行,相乘后的结果。

样例输入:

123456789
123456789

样例输出:

15241578750190521

代码:

#include <bits/stdc++.h>

using namespace std;


int main()
{
    string a,b;
    cin>>a>>b;
    long long int temp[100000],result[100000];
    fill(result,result+sizeof(result)/sizeof(long long int),0);
    int total_weishu = 0;
    for(int i=0; i<b.length(); i++)
    {
        memset(temp,0,sizeof(temp));


        long long int up = 0;
        int temp_weishu = a.length()+i;
        for(int j=0; j<a.length(); j++)
        {
            up += (b[b.length()-i-1]-'0') * (a[a.length()-j-1]-'0');
            temp[i+j] = up%10;
            up/=10;
        }
        while(up)
        {
            temp[temp_weishu] = up%10;
            temp_weishu++;
            up/=10;
        }


        int result_weishu = 0;
        for(int j=0; j<temp_weishu; j++)
        {
            result[j] = result[j] + temp[j];
            result[j+1] += result[j]/10;
            result[j] = result[j]%10;
            if(result[j+1])
            {
                result_weishu = j+2;
            }
            else {
                result_weishu = j+1;
            }
        }
        total_weishu = max(result_weishu, total_weishu);


    }
    for(int i=total_weishu; i; i--)
    {
        cout<<result[i-1];
    }


    return 0;
}

标签:大数,int,非负,long,ICPC,result,123456789,sizeof,乘法
来源: https://blog.csdn.net/draxbory/article/details/122770685