大数乘法

本文介绍了一种使用字符串存储大数并进行乘法运算的方法。通过双循环相乘及中间结果叠加的方式,解决了计算机单一类型无法存储超长数字的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大数乘法

很多时候会用到 大数字 相乘。

数字太大了之后超出计算机单一类型的最大存储长度会比较麻烦

所以考虑用字符串来存储,这样对长度就没有了限制

本文提供一种简单的处理方式。。甚至称不上一种算法,其实就是分解了一下我们平时的运算过程

思路就是先双循环去乘,再用二维数组去保存中间结果。

最后将中间结果叠加就可以

 


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

#ifdef UNICODE
#define STD_STRING wstring
#else
#define STD_STRING string
#endif // UNICODE

int char2num(TCHAR c)
{
	return c - _T('0');
}

TCHAR num2char(int n)
{
	return (_T('0') + n);
}

STD_STRING GetMultiplicationResult(STD_STRING strValue1, STD_STRING strValue2)
{
	STD_STRING strTempv1 = strValue1;
	STD_STRING strTempv2 = strValue2;
	STD_STRING strResult;
	reverse(strTempv1.begin(), strTempv1.end());
	reverse(strTempv2.begin(), strTempv2.end());
	vector<vector<int>> Table_TempResult;
	for (int i = 0; i < strTempv1.size(); i++)
	{
		int iHigh = 0;
		int iLow = 0;
		vector<int> vecRow;
		for (int g = i; g > 0; g--)
			vecRow.push_back(0);
		for (int j = 0; j < strTempv2.size(); j++)
		{
			iLow = (char2num(strTempv2[j]) * char2num(strTempv1[i]) + iHigh) % 10;
			iHigh = (char2num(strTempv2[j]) * char2num(strTempv1[i]) + iHigh) / 10;
			vecRow.push_back(iLow);
		}
		if (iHigh > 0)
		{
			vecRow.push_back(iHigh);
		}

		Table_TempResult.push_back(vecRow);
	}
	vector<int> vectResult;
	for (int i = 0; i < Table_TempResult.size(); i++)
	{
		int iHigh = 0;
		int iLow = 0;
		vector<int> vectTemp;
		if (i==0)
			vectTemp = Table_TempResult[i];
		else
		{
			int nsize = Table_TempResult[i].size() > vectResult.size() ? Table_TempResult[i].size() : vectResult.size();
			for (int j = 0; j < nsize; j++)
			{
				if (j < vectResult.size() && j < Table_TempResult[i].size())
				{
					iLow = (vectResult[j] + Table_TempResult[i][j] + iHigh) % 10;
					iHigh = (vectResult[j] + Table_TempResult[i][j] + iHigh) / 10;
				}
				else
				{
					vector<int> v = Table_TempResult[i].size() > vectResult.size() ? Table_TempResult[i] : vectResult;

					iLow = (v[j] + iHigh) % 10;
					iHigh = (v[j] + iHigh) / 10;
				}
				

				vectTemp.push_back(iLow);
			}
			if (iHigh > 0)
				vectTemp.push_back(iHigh);
		}

		vectResult = vectTemp;	
	}
	for (int i = 0; i < vectResult.size(); i++)
	{
		strResult.push_back(num2char(vectResult[i]));
	}

	reverse(strResult.begin(), strResult.end());
	return strResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
	STD_STRING strResult = GetMultiplicationResult(_T("78945612306"), _T("74185296303"));
	wcout << strResult.c_str() << endl;
	system("pause");
	return 0;
}

数据对齐版本

// BigNumberCalc.cpp : Defines the entry point for the console application.
//

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

#ifdef UNICODE
#define STD_STRING wstring
#else
#define STD_STRING string
#endif // UNICODE

int char2num(TCHAR c)
{
	return c - _T('0');
}

TCHAR num2char(int n)
{
	return (_T('0') + n);
}

STD_STRING GetMultiplicationResult(STD_STRING strValue1, STD_STRING strValue2)
{
	STD_STRING strTempv1 = strValue1;
	STD_STRING strTempv2 = strValue2;
	STD_STRING strResult;
	reverse(strTempv1.begin(), strTempv1.end());
	reverse(strTempv2.begin(), strTempv2.end());
	vector<vector<int>> Table_TempResult;
	for (int i = 0; i < strTempv1.size(); i++)
	{
		int iHigh = 0;
		int iLow = 0;
		vector<int> vecRow;
		for (int g = i; g > 0; g--)
			vecRow.push_back(0);
		for (int j = 0; j < strTempv2.size(); j++)
		{
			iLow = (char2num(strTempv2[j]) * char2num(strTempv1[i]) + iHigh) % 10;
			iHigh = (char2num(strTempv2[j]) * char2num(strTempv1[i]) + iHigh) / 10;
			vecRow.push_back(iLow);
		}
		if (iHigh > 0)
		{
			vecRow.push_back(iHigh);
		}

		Table_TempResult.push_back(vecRow);
	}
	vector<int> vectResult;
	int nMaxSize = 0;
	for (int i = 0; i < Table_TempResult.size(); i++)
	{
		if (Table_TempResult[i].size() > nMaxSize)
			nMaxSize = Table_TempResult[i].size();
	}

	for (int i = 0; i < Table_TempResult.size(); i++)
	{
		//Align data
		if (Table_TempResult[i].size() < nMaxSize)
		{
			int nNeed = nMaxSize - Table_TempResult[i].size();
			for (int j = 0; j < nNeed; j++)
				Table_TempResult[i].push_back(0);

		}

		int iHigh = 0;
		int iLow = 0;
		vector<int> vectTemp;
		if (i==0)
			vectTemp = Table_TempResult[i];
		else
		{
			int nsize = Table_TempResult[i].size();
			for (int j = 0; j < nsize; j++)
			{
				iLow = (vectResult[j] + Table_TempResult[i][j] + iHigh) % 10;
				iHigh = (vectResult[j] + Table_TempResult[i][j] + iHigh) / 10;
				
				vectTemp.push_back(iLow);
			}
			if (iHigh > 0)
				vectTemp.push_back(iHigh);
		}

		vectResult = vectTemp;	
	}
	//clear data
	int bigsize = vectResult.size();
	for (int i = bigsize-1; i >= 0; i++)
	{
		if (vectResult[i] == 0)
			vectResult.pop_back();
		else
			break;
	}

	for (int i = 0; i < vectResult.size(); i++)
	{
		strResult.push_back(num2char(vectResult[i]));
	}

	reverse(strResult.begin(), strResult.end());
	return strResult;
}

int _tmain(int argc, _TCHAR* argv[])
{
	STD_STRING strResult = GetMultiplicationResult(_T("78945612306321"), _T("74185296303"));
	wcout << strResult.c_str() << endl;
	system("pause");
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值