大数乘法 10106 - Product

UVa OJ

Root :: AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 1. Elementary Problem Solving :: Big Number

 Product 

The Problem

The problem is to multiply two integers X, Y. (0<=X,Y<10250)

The Input

The input will consist of a set of pairs of lines. Each line in pair contains one multiplyer.

The Output

For each input pair of lines the output line should consist one integer the product.

Sample Input

12
12
2
222222222222222222222222

Sample Output

144
444444444444444444444444

大数乘法。通过一个个模拟运算

用两个循环来模拟乘法运算。

因为一个小细节,把运算的过程写错了。。wa了好多次

然后没有考虑到乘数为0.。。又贡献了几次wa

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctype.h>
char numa[300], numb[300];
int mul[600];
using namespace std;
int main ()
{
    int i,j,max;
    while(cin>>numa>>numb)
    {
        memset(mul,0,sizeof(mul));
        int lena=strlen(numa),lenb=strlen(numb);;
        for (i=0, j=lena-1; i<j; i++, j--)
        {
            char temp;
            temp=numa[i];
            numa[i]=numa[j];
            numa[j]=temp;
        }
        for (i=0, j=lenb-1; i<j; i++, j--)
        {
            char temp;
            temp=numb[i];
            numb[i]=numb[j];
            numb[j]=temp;
        }
        for (i=0; i<lenb; i++)
        {
            for (j=0; j<lena; j++)
            {
                mul[j+i]+=(numa[j]-'0')*(numb[i]-'0');
                if (mul[j+i]>=10)
                {
                    mul[j+i+1]+=mul[j+i]/10; //之前写成了mul[j+i+1]+=mul[j+i+1]/10; 一直wa。。。
                    mul[j+i]=mul[j+i]%10;
                }
            }
        }
        max=j+i+3;
        for (j=max; j>=0; j--)
            if (mul[j]!=0) break;
        if (j>=0)       //要考虑前置0的问题
            for (i=j; i>=0; i--)
                cout<<mul[i];
        else cout<<"0";
        cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值