如果这两个数比较小(即能用计算机的int、long类型表示,并且相乘后的结果任然能够表示)那么直接用计算机
的乘法运算就可以了。这里的大数指的是用计算机的预设类型(int、long等)无法表示的数,如:11111111111
1111111111111111111111111111111111111111111111111111111111111111111111111111111111111...
算法原理:这个原理其实很简单,就是我们小学学的算术,两个数相乘把其中一个数的各位数字乘以另一个数,并
把相乘的结果累加,注意在累加时要补0,比如:一个数的十位上的数乘以另一个数这时计算结果要乘以10,在这里
我们用字符串保存大数,算法模拟了两个数相乘的演算过程,最终计算出结果。

< http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">//大数乘法 std::string bigDataMultiply(string str1, string str2) { const char* data1 = str1.data(); const char* data2 = str2.data(); int len1=str1.length(); int len2=str2.length(); std::vector
result; //将第一个数分解 for (int i = len1 - 1; i >= 0; i--) { int c1 = data1[i] - '0'; int cur = 0; int next = 0; std::vector
str1; //第一数的每一位乘以第二个数 for (int j = len2 - 1; j >= 0; j--) { int c2 = data2[j] - '0'; int res = c1 * c2; //向前进位 if (next != 0) res += next; if (res >= 10) { cur = res % 10; next = res / 10; } else { cur = res; next = 0; } str1.push_back(char('0' + cur)); } //判断最后是否需要进位 if (next != 0) str1.push_back(char('0' + next)); if (result.size() == 0) { for (size_t k = 0; k < str1.size(); k++){ result.push_back(str1[k]); } } else { //除了第一个数位其他的数后面要添一个0 for (int k = 0; k < len1 - 1 - i; k++) str1.insert(str1.begin(), '0'); //大数相加,将结果保存在str1中 int one = 0; int two = 0; int k = result.size(); int h = 0; int num = str1.size(); while (h < num) { int tc; if (k > 0) tc = (str1[h] - '0') + (result[h] - '0'); else tc = str1[h] - '0'; if (two != 0) tc += two; if (tc >= 10) { one = tc % 10; two = tc / 10; } else { one = tc; two = 0; } str1[h]=char(one + '0'); h++; k--; } //判断最后一位是否需要进一 if (two != 0) str1.push_back(char(two + '0')); result.clear(); for (size_t k = 0; k < str1.size(); k++){ result.push_back(str1[k]); } } } //计算的时候是逆序的,所以需要逆序输出 int resultLen = result.size(); string resStr; for (int i = 0; i < resultLen; i++) { resStr.push_back(result[resultLen-1-i]); } return resStr; }平常用C#和
Java比较多,这个是本人今天上班的时候闲来无事用C++实现的,
,可能很久没用C++了实现的时候有点生疏了,如有问题,欢迎批评指正!