[英]how to multiply 2 large numbers?
i created a small console app that multiply 2 long int number. 我创建了一个小型控制台应用程序,该应用程序将2长整型数相乘。 i don't know where my problem is.
我不知道我的问题在哪里。 this app work fine until the number of digits is 3.
这个应用程式可以正常运作,直到位数为3。
but if number of digit was bigger than 3 , the app's output is wrong. 但是如果数字位数大于3,则应用程序的输出错误。 :(
:(
please show me where my problem is that i solve it. 请告诉我我的问题在哪里,我可以解决它。
here is my code: 这是我的代码:
int digits (int n)
{
int counter = 0;
while (n > 0)
{
n/=10;
counter++;
}
return counter;
}
long longMultiply(long a, long b)
{
const int S = 3;
int w,x,y,z;
int n = max(digits(a),digits(b));
if(a == 0 || b ==0) {
return 0;
} else if (n <= S) {
return a*b;
} else {
int m = (n/2);
//first number
x = a/(10^m);
y = a%(10^m);
//second number
w = b/(10^m);
z = b%(10^m);
return (longMultiply(x,w)*(10^(2*m)) + (longMultiply(x,z) + longMultiply(w,y)))*(10^m) + longMultiply(y,z) ;
}
}
int main() {
//digits(12345);
cout << longMultiply(100,100);
return 0;
}
10^m is not the m-th power of 10, in fact this is 10 xor'ed by m 10 ^ m不是10的第m次幂,实际上这是m的10异或
You can use the pow
function from cmath library instead ( http://www.cplusplus.com/reference/cmath/pow/ ), but it works on floating-point numbers. 您可以改用cmath库中的
pow
函数( http://www.cplusplus.com/reference/cmath/pow/ ),但是它适用于浮点数。
Alternatively to get 10^m, you could simply multiply 1 m times by 10. 另外,要获得10 ^ m,您可以将1 m乘以10。
int m = (n/2);
long tenToM = 1;
for (int i=0; i<m; i++)
tenToM *= 10;
long tenToTwoM = tenToM * tenToM;
and then instead of 10^m
use tenToM
and instead of 10^(2*m)
use tenToTwoM
然后代替
10^m
使用tenToM
和代替10^(2*m)
使用tenToTwoM
It seems like your problem is in the logic of the else portion. 看来您的问题出在else部分的逻辑上。 It works up to 3 digits because it is simply outputting the product when that fails it runs your else block which I am not sure I understand.
它最多可以显示3位数字,因为它在失败时仅输出产品,因此运行您的else块,我不确定我是否理解。 What exactly is setting
m = n/2
trying to do? 设置
m = n/2
到底要做什么?
If the product is less than or equal to 10 ^ 18 ; 如果乘积小于或等于10 ^ 18; you can simply use
你可以简单地使用
long long product = a * b ;
If a or b are greater than the range of long long ; 如果a或b大于long long的范围; one can simply take one as long long and another as string .
一个人可以简单地取一个长而另一个长的字符串。 Suppose a > 10^18 and b < 10^18 .
假设a> 10 ^ 18和b <10 ^ 18。 The below code is valid only when b * 9 is in the range of long long .
仅当b * 9在long long范围内时,以下代码才有效。
string a ; long long b ;
cin >> a >> b ;
reverse ( a.begin() , a.end() ) ;
string prod ;
long long temp ,carry ;
temp = carry = 0 ;
for ( i = 0 ; i < a.length() ; i++ ){
temp = (a[i] - '0') * b + carry ;
prod += ( temp % 10 ) + '0' ;
carry = temp / 10 ;
}
while ( carry != 0 ){
prod += ( carry % 10 ) + '0' ;
carry /= 10 ;
}
reverse ( prod.begin() , prod.end() ) ;
cout << prod ; // this string contains the required product .
However if both are very big you can consider using a third party Big Integer Library. 但是,如果两者都很大,则可以考虑使用第三方的Big Integer Library。 For external Big Integer Library you can consider using BOOST BigInteger Library , which is quite fast and highly tested.
对于外部Big Integer库,您可以考虑使用BOOST BigInteger库,该库非常快速且经过了严格的测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.