php实现俄罗斯乘法实例_php技巧

WBOY
Release: 2016-05-16 20:21:42
Original
1048 people have browsed it

本文实例讲述了php实现俄罗斯乘法的方法。分享给大家供大家参考。具体分析如下:

一、概述:

俄罗斯乘法是一种计算两数相乘的算法。
举例如下:
计算 35*72
过程
35 72
17 144
8 288
4 576
2 1152
1 2304
从上到下,对每一行,若左边的数字若为奇数,则将右边的数字取出,累加。
72+144+2304=2520
累加的结果2520即为乘积。

二、实现代码:

<&#63;php
function russian($m, $n, $res = 0){
  (1 == ($n & 1)) && $res += $m;
  $m = $m << 1;
  $n = $n >> 1;
  return $n &#63; russian($m, $n, $res) : $res;
}
echo russian(7, 8);
Copy after login

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template