大数乘法
很多时候会用到 大数字 相乘。
数字太大了之后超出计算机单一类型的最大存储长度会比较麻烦
所以考虑用字符串来存储,这样对长度就没有了限制
本文提供一种简单的处理方式。。甚至称不上一种算法,其实就是分解了一下我们平时的运算过程
思路就是先双循环去乘,再用二维数组去保存中间结果。
最后将中间结果叠加就可以
#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;
}