51nod1027---大数乘法(51nod基础:乘法模拟)

【题目来源】https://www.51nod.com
【题意】
简单的大数乘法。模板。。
【思路】
强行模拟,,,。
【代码】

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
char strOne[1000+10];
char strTwo[1000+10];
int  arrAns[2000+10][2000+10];
int arrOne[1000+10];
int arrTwo[1000+10];
int res[2000+10];
int main()
{
    scanf("%s",strOne);
    scanf("%s",strTwo);
    int lenOne=strlen(strOne);
    int lenTwo=strlen(strTwo);
    for(int i=lenOne-1; i>=0; i--)
        arrOne[i]=strOne[lenOne-i-1]-'0';
    for(int i=lenTwo-1; i>=0; i--)
        arrTwo[i]=strTwo[lenTwo-i-1]-'0';
    memset(arrAns,0,sizeof(arrAns));
    for(int i=0; i<lenOne; i++)
    {
        int One=arrOne[i];
        for(int j=0; j<lenTwo; j++)
        {
            int Two=arrTwo[j];
            int ans=arrAns[i][j]+One*Two;
            arrAns[i][j+1]+=ans/10;
            arrAns[i][j]=ans%10;
        }
    }//每一位都乘
    for(int j=0; j<=2*lenTwo; j++)//加起来
    {
        int p=0,q=j;
        while(1)
        {
            res[j]+=arrAns[p][q];
            p++;
            q--;
            if(p>lenOne||q<0)
            {
                break;
            }
        }
    }
    for(int i=0; i<2*lenTwo; i++)//进位
    {
        if(res[i]>9)
        {
            res[i+1]+=res[i]/10;
            res[i]%=10;
        }
    }
    int k;
    for(k=2*lenTwo; k>=0&&res[k]==0; k--);
    if(k==-1)
        printf("0\n");//特判
    else
    {
        for(int i=k; i>=0; i--)
            printf("%d",res[i]);
        printf("\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值