码迷,mamicode.com
首页 > 其他好文 > 详细

大数乘法 (poj2389)

时间:2014-08-01 23:15:52      阅读:309      评论:0      收藏:0      [点我收藏+]

标签:os   io   for   ar   html   htm   ios   har   

模板

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
void cheng( char *a, char *b, char *sum )
{
	int temp[2500];
	int lena,lenb,l;
	lena=strlen(a);
	lenb=strlen(b);
	int len = lena + lenb;
	for (int i=0;i<=len;i++ )
		temp[i]=0;

	for (int i=lena-1;i>=0;i-- )
        {
		     l=lena-1-i;
		for (int j=lenb-1;j>=0;j-- )
		{
			temp[l]=(b[j]-'0')*(a[i]-'0')+temp[l];
			l++;
		}
	}

	while (temp[l]==0)
		l--;
	for (int i=0;i<=l;i++ )
        {
		temp[i+1]+=temp[i]/10;
		temp[i]=temp[i]%10;
	    }
	if ( temp[l+1]!=0 )
		l++;

	while ( temp[l]/10!=0 )
    {
		temp[l+1]+=temp[l]/10;
		temp[l]=temp[l]%10;
		l++;
	}
	if ( temp[l]==0 )
		l--;
    int i=0;
	while ( l>=0 )
	{
		sum[i]=temp[l]+'0';
		i++;
		l--;
	}
	sum[i]='\0';
}
int main()
{
    char a[10000],b[10000],sum[200000];
    cin>>a>>b;
    cheng(a,b,sum);
    puts(sum);
    return 0;
}


大数乘法 (poj2389),布布扣,bubuko.com

大数乘法 (poj2389)

标签:os   io   for   ar   html   htm   ios   har   

原文地址:http://blog.csdn.net/wjw0130/article/details/38341725

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!