【基础】高精度乘法

描述 Description 
 高精度乘法
输入:两行,每行表示一个非负整数(不超过10000位)
输出:两数的乘积。
   
   
  样例输入 Sample Input 
 
   
   
  样例输出 Sample Output 
 
   
   
  时间限制 Time Limitation 
 各个测试点1s


我又菜了,交了两次才过。都是不做数据的错。

是因为读入数据压位写错了,对于bit的整倍数长度的数,要先把长度+1,因为会跳过一开始的零头位处理。


#include <cstdio>
#include <string>
#include <cstring>

typedef long long ll;

const long bit = 8;
const ll bit10 = 100000000;

void pocess(char* x,ll* y)
{
	long l = 0;
	while (x[++l]);l--;
	y[0] = l/bit+1;

	long rr = l%bit;
	for (long i=1;i<rr+1;i++)
	{
		y[y[0]] = (y[y[0]]<<3)+(y[y[0]]<<1)+x[1]-'0';
		x++; l--;
	}

	rr = y[0];
	for (long i=1;i<l+1;i++)
	{
		if (!((i-1) % bit)) rr --;
		y[rr] = (y[rr]<<3)+(y[rr]<<1)+x[1]-'0';
		x++;
	}
}

void multiply(ll* a,ll* b,ll* c)
{
	c[0] = a[0] + b[0];
	for (long i=1;i<a[0]+1;i++)
	{
		for (long j=1;j<b[0]+1;j++)
		{
			c[i+j-1] += a[i]*b[j];
			while (c[i+j-1] >= bit10)
			{
				c[i+j] += c[i+j-1]/bit10;
				c[i+j-1] %= bit10;
			}
		}
	}
	while (!c[c[0]])c[0]--;
}

void output(ll* a)
{
	printf("%ld",long(a[a[0]]));
	for (long i=a[0]-1;i>0;i--)
	{
		printf("%08ld",long(a[i]));
	}
}

char aa[10010];
char bb[10010];
ll a[10010];
ll b[10010];
ll c[20010];

int main()
{
	freopen("multiply.in","r",stdin);
	freopen("multiply.out","w",stdout);

	scanf("%s",aa+1);
	scanf("%s",bb+1);

	pocess(aa,a);
	pocess(bb,b);

	multiply(a,b,c);

	output(c);

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值