两个矩阵相乘的乘法次数

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 :

问题是我们有两个整数,并且不使用乘法运算符就可以找到它们的乘法 。 使用俄罗斯农民算法可以解决此问题。 假设两个给定的数字是m和n 。 用0初始化mul并在n大于零时重复以下步骤:

  1. Add m to mul, if n is odd

    如果n为奇数,则将m加到mul中

  2. 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

参考: 俄罗斯农民繁殖

C ++程序实现俄罗斯农民算法 (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 odd
if (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

两个矩阵相乘的乘法次数

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

  1. c语言编写两个矩阵的乘积,如何用c语言编写两个模糊矩阵相乘的程序?

    满意答案 有三处,你仔细看一下#define M 4 #include float min(float x,float y) { return(x }; float max(float*p,int n ...

  2. c ++ 链表_C ++程序查找两个单个链表的并集

    c ++ 链表 Problem statement: Write a C++ program to find the union of two single linked lists. 问题陈述:编写 ...

  3. c++ 取两个链表的交集_使用C ++程序查找两个链表的交集

    c++ 取两个链表的交集 Problem statement: Write a C++ program to find the intersection of two single linked li ...

  4. c语言for循环++_C ++程序使用循环查找数字的幂

    c语言for循环++ Here, we are going to calculate the value of Nth power of a number without using pow func ...

  5. 用c语言编写两个数的最小公倍数,用C语言编写程序求两个数的最小公倍数,并输出...

    如图使用辗转相除法求最小公倍数: 方法步骤: 一.打开VC2010(或其他C语言编译器),新建项目-选择Win32为控制台应用程序-命名-确定 二.选择源文件-添加-新建项 三.选择C++文件-命名. ...

  6. 矩阵乘法(两个矩阵相乘)

    Description 给定两个矩阵$A$和$B$,你需要判断它们是否可以相乘,若可以请输出相乘后的矩阵. Input 第一行输入一个整数$T$,代表有$T$组测试数据. 每组数据第一行输入两个整数$ ...

  7. C语言求任意两个矩阵相乘的算法(初学尝试矩阵乘法)

    C语言求任意两个矩阵相乘的算法(不同于大部分规格固定的矩阵乘法) 结果图如下   : 代码如下: //----- 任意两个矩阵相乘 # include <stdio.h> int main ...

  8. 矩阵乘法次数的计算过程

    矩阵乘法次数的计算: 以两个矩阵相乘为例,A1xA2,A1和A2为两个矩阵,假设A1的行列数是pxq,A2的行列数是qxr.那么对于A1xA2而言,我们需要分别执行pxr次对应A1的行元素乘以A2的列 ...

  9. 用两个矩阵的点积计算神经网络的迭代次数 2-8

    每个神经网络对应每个收敛标准δ都有一个特征的迭代次数n,因此可以用迭代次数曲线n(δ)来评价网络性能. 在<神经网络的迭代次数是一个线性的变量吗?>中得到表达式 一个二分类网络分类两个对象 ...

最新文章

  1. P4588 [TJOI2018]数学计算(线段树维护区间乘和单点修改)
  2. 2018.07.17 洛谷P1368 工艺(最小表示法)
  3. linux 快速删除大量/大文件
  4. 有关C++多态的一些测试
  5. android 之ViewStub
  6. [WPF系列]-Deep Zoom
  7. sql语句按月份统计查询
  8. 技术揭秘:华为云DLI背后的核心计算引擎
  9. 4chan 爬虫_类似4chan网站
  10. Windows USB驱动开发点滴积累备忘录
  11. iphone按钮圆角的问题
  12. spring-cloud-sleuth 和 分布式链路跟踪系统
  13. IDEA web项目导出 war 包
  14. Linux C语言编程学习材料
  15. java编程加载窗口,插入图片
  16. 分享200个App移动端模板
  17. 软件工程毕设(四)·调研报告
  18. wireshark抓包并复原图像
  19. 一文到胃------合并(归并)排序原理
  20. 如何在比赛和项目中培养一个好的探索性分析(EDA)思维 —— 翻译自kaggle一位有趣的分享者

热门文章

  1. java专业术语 ioc_什么叫IOC(编程术语
  2. virtualbox的USB识别
  3. 【PHP 扩展开发】Zephir 基础篇
  4. 由mysql8降级到mysql5
  5. 相约11月25日,开发者的嘉年华
  6. Exploit开发系列教程-Mona 2 SEH
  7. maven安装及集成myeclipse
  8. NFS部署及优化(一)
  9. 解决outlook2013设置错误无法启动
  10. 数据结构与算法面试题80道(32)