加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数乘法

发布时间:2020-12-14 03:09:43 所属栏目:大数据 来源:网络整理
导读:给出2个大整数A,B,计算A*B的结果。 Input 第1行:大数A第2行:大数B(A,B的长度?=?1000,A,B?=?0) Output 输出A?*?B Input示例 123456234567 Output示例 28958703552 #include iostream#include algorithm#include string.husing namespace std;char a[1000
给出2个大整数A,B,计算A*B的结果。
Input
第1行:大数A
第2行:大数B
(A,B的长度?<=?1000,A,B?>=?0)
Output
输出A?*?B
Input示例
123456
234567
Output示例
28958703552

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;

char a[1000];
char b[1000];
char result[3000];

void fun(int m,int n)
{
	int len = 0;
	int pos = 0;

	memset(result,sizeof(result));
	for (int i = m-1; i >= 0; i--)
	{
		int d = pos;
		for (int j = n-1; j >= 0; j--)
		{
			int temp = a[i] * b[j];
			temp += result[d];
			result[d] = temp % 10;
			result[d+1] += temp / 10;
			d++;
		}
		pos++;
	}

	bool found = false;
	for (int i = m+n+1; i >= 0; i--)
	{
		if (result[i] != 0)
		{
			found = true;
			cout << (int)result[i];
		}
		else if (found)
		{
			cout << (int)result[i];
		}
	}
}

int main()
{
	cin >> a >> b;
	int m = strlen(a);
	int n = strlen(b);
	for (int i = 0; i < m; i++)
	{
		a[i] -= '0';
	}

	for (int j = 0; j < n; j++)
	{
		b[j] -= '0';
	}
	fun(m,n);

    return 0;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读