日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符

發(fā)布時間:2025/3/11 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 两个矩阵相乘的乘法次数_C ++程序将两个数字相乘而不使用乘法运算符 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

兩個矩陣相乘的乘法次數(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)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。