高精度模板之高精乘高精

话说就要noip了,前两天模拟赛考了一道需要压位的高精乘高精,然后我发现高精度乘法还没有模板。话说我的写法很难看吧!因为发现考试时高精度都不是单纯的计算,又刚好学过了重载运算符,所以感觉这样写可能会方便一些吧。不管怎样,至少应该还是够用了。如果亲们看不惯就当我不存在即可。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
char A[402],B[402];
struct shu{int s[1602],l;} a,b,c;
void pre()
{
	a.l=strlen(A+1); b.l=strlen(B+1);
	int i,t;
	for(i=1;i<=a.l;i++)
	   {t=A[i]-'0'; a.s[i]=A[a.l-i+1]-'0'; a.s[a.l-i+1]=t;}
	for(i=1;i<=b.l;i++)
	   {t=B[i]-'0'; b.s[i]=B[b.l-i+1]-'0'; b.s[b.l-i+1]=t;}
}
shu operator * (const shu &x,const shu &y)
{
	shu ans;
	memset(ans.s,0,sizeof(ans.s));
	int i,j;
	for(i=1;i<=x.l;i++)
	for(j=1;j<=y.l;j++)
	   ans.s[i+j-1]+=x.s[i]*y.s[j];
	ans.l=x.l+y.l-1;
	for(i=1;i<=ans.l;i++)
	   {if(ans.s[i]>=10)
	       {ans.s[i+1]+=ans.s[i]/10;
		    ans.s[i]%=10;
		   }
	   }
	i=ans.l+1;
	while(ans.s[i]>0)
	   {if(ans.s[i]>=10)
	       {ans.s[i+1]+=ans.s[i]/10;
		    ans.s[i]%=10;
		   }
		i++;
	   }
	ans.l=i-1;
	return ans;
}
int main()
{
	while(scanf("%s%s",A+1,B+1)!=EOF)
	   {pre(); 
	    c=a*b;
	    for(int i=c.l;i>0;i--)
	       printf("%d",c.s[i]);
	    printf("\n");
	   }
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值