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,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;
}