poj2389

第一道高精度乘法

交的第一遍WA了,然后在讨论区找了一组测试数据

435234123
9845672341334565
结果应该是

4285172566826106047361495

然而却输出了‘-’

想到应该是数据溢出了,之前将str_c定义成了char类型了,改成int后AC

/**
* @file source.cpp
* @author hybest
* @date 2013-07-17
* @description
* ACM/ICPC train: Bull Math
*/

#include<stdio.h>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
#include<string.h>
#define INF 1024

using namespace std;

char str_a[INF], str_b[INF];
int str_c[INF];
int la, lb, lmax;

void input()
{
	int i, temp;
	memset( str_a, 0, INF);
	memset( str_b, 0, INF);
	memset( str_c, 0, INF);
	scanf( "%s %s", str_a, str_b );
	la = strlen(str_a);
	lb = strlen(str_b);
	for( i = 0; i < la; i++ )
	{
		str_a[i] = str_a[i] - '0';
	}
	for( i = 0; i < lb; i++ )
	{
		str_b[i] = str_b[i] - '0';
	}
	for( i = 0; i < la - 1 - i; i++ )
	{
		temp = str_a[i];
		str_a[i] = str_a[ la - 1 - i ];
		str_a[ la - 1 - i ] = temp;
	}
	for( i = 0; i < lb - 1 - i; i++ )
	{
		temp = str_b[i];
		str_b[i] = str_b[ lb - 1 - i ];
		str_b[ lb - 1 - i ] = temp;
	}
	return;
}

void multi()
{
	int i, j;
	for( i = 0; i < la; i++ )
	{
		for( j = 0; j < lb; j++ )
		{
			str_c[i+j] += str_a[i] * str_b[j];
		}
	}
	lmax = la + lb;
	for( i = 0; i < lmax; i++ )
	{
		str_c[i+1] += str_c[i] / 10;
		str_c[i] = str_c[i] % 10;
	}
	while( str_c[lmax] == 0 && lmax > 0 )
	{
		lmax--;
	}
	i = str_c[lmax];
}

int main()
{
	int i;
	input();
	multi();
	for( i = lmax; i >= 0; i-- )
	{
		printf( "%d", str_c[i] );
	}
	printf( "\n" );
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值