poj2389 大数问题-大数相乘

以前写的真垃圾

直接贴模板

 

#include<iostream>
#include<string>
using namespace std;

const int size=1000;  //大数位数

void mult(char* A,char* B,char* ans)
{
	int a[size+1]={0};
	int b[size+1]={0};
	int pa=0,pb=0;
	int c[2*size+1]={0};

	int lena=strlen(A);
	int lenb=strlen(B);

	for(int i=lena-1;i>=0;i--)
		a[pa++]=A[i]-'0';
	for(int j=lenb-1;j>=0;j--)
		b[pb++]=B[j]-'0';

	for(pb=0;pb<lenb;pb++)
	{
		int w=0;  //低位到高位的进位
		for(pa=0;pa<=lena;pa++)
		{
			int temp=a[pa]*b[pb]+w;
			w=temp/10;
			temp=(c[pa+pb]+=temp%10);
			c[pa+pb]=temp%10;
			w+=temp/10;
		}
	}
	bool flag=false;
	bool sign=false;  //标记ans是否为全0
	for(pa=0,pb=lena+lenb-1;pb>=0;pb--)
	{
		if(!flag && c[pb]==0)  //删除ans开头的0
			continue;
		else
			flag=true;

		sign=true;
		ans[pa++]=c[pb]+'0';
	}
	if(sign)
		ans[pa]='\0';
	else
	{
		ans[0]='0';
		ans[1]='\0';
	}

	return;
}

int main(void)
{
	char a[size+1];   //被乘数
	char b[size+1];   //乘数
	char ans[2*size+1];  //结果
	while(cin>>a>>b) 
	{
		mult(a,b,ans);
		cout<<ans<<endl;
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值