两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符
兩個矩陣相乘的乘法次數(shù)
The problem is we have two integer numbers and find the multiplication of them without using the multiplication operator. This problem can be solved using the Russian peasant algorithm. Assume the two given numbers are m and n. Initialize mul with 0 and repeat following steps while n is greater than zero :
問題是我們有兩個整數(shù),并且不使用乘法運算符就可以找到它們的乘法 。 使用俄羅斯農(nóng)民算法可以解決此問題。 假設兩個給定的數(shù)字是m和n 。 用0初始化mul并在n大于零時重復以下步驟:
Add m to mul, if n is odd
如果n為奇數(shù),則將m加到mul中
Double the value of m and half the value of n.
將m的值加倍,將n的值減半。
Here we use properties of << (left shift) and >> (right shift) operators for doubling the value of m and for dividing the value of ‘n by 2.
在這里,我們使用<< (左移)和>> (右移)運算符的屬性將m的值加倍,并將' n的值除以2。
Reference: Russian peasant multiplication
參考: 俄羅斯農(nóng)民繁殖
C ++程序?qū)崿F(xiàn)俄羅斯農(nóng)民算法 (C++ program to implement Russian peasant algorithm)
#include <iostream> using namespace std; int Multiply(int m, int n) {int mul=0; while (n > 0) {// if n is oddif (n & 1) mul = mul + m; // Double 'm' and halve 'n' m = m << 1; n = n >> 1; } return mul; } int main() {int ans;ans=Multiply(2,3);cout<<"Multiplication of 2 and 3 = "<<ans<<endl;ans=Multiply(10,10);cout<<"Multiplication of 10 and 10 = "<<ans<<endl;return 0; }Output
輸出量
Multiplication of 2 and 3 = 6 Multiplication of 10 and 10 = 100翻譯自: https://www.includehelp.com/cpp-programs/multiply-two-numbers-without-using-multiplication-operator.aspx
兩個矩陣相乘的乘法次數(shù)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java LocalDate类| len
- 下一篇: code craft_Craft.io调