Home  >  Article  >  Backend Development  >  How to implement addition in PHP without using addition, subtraction, multiplication and division symbols

How to implement addition in PHP without using addition, subtraction, multiplication and division symbols

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-07-09 15:05:371959browse

We know that to implement addition we need to use arithmetic symbols, so how to implement addition without using arithmetic symbols? Today, the editor will introduce how to implement addition without using addition, subtraction, multiplication and division operators. You can refer to it if you need it.

How to implement addition in PHP without using addition, subtraction, multiplication and division symbols

Write a function to find the sum of two integers. It is required that the four arithmetic symbols " ", "-", "*" and "/" must not be used in the function body.

Example:

输入: a = 1, b = 1
输出: 2

Tips:

a, b may be negative or 0, the result will not overflow the 32-bit integer

Solve the problem Idea 1

array_sum function

Code

class Solution {
    /** 
    * 递归法 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        // return bcadd($a, $b); // bc 系列函数
        return array_sum([$a, $b]);
    }}

Solution idea 2 - With the help of bit operations

this question bit It's better to memorize the operations. After all, the analog addition usage of bit operations is basically just this question, and it can be easily forgotten. . . . .

^ Or - equivalent to summation without carry, imagine the simulation situation in base 10: (for example: 19 1 = 20; sum without carry is 10, not 20; because it does not care Carry situation)

& and - are equivalent to finding the number of carries for each bit. First look at the definition: 1 & 1 = 1; 1 & 0 = 0; 0 & 0 = 0; that is, when both are 1 is only 1, which can just simulate the situation of the carry number, or imagine the simulation situation in base 10: (9 1 = 10, if it is processed with the idea of ​​​​&, then the carry number obtained by 9 1 is 1, not 10, So use <<1 to move one position to the left, so it becomes 10);

The formula is: (a^b) ^ ((a&b)<<1) That is : Find the number of carries each time without carry - We need to repeat this process until the number of carries is 0;

Code

class Solution {
    /** 
    * 递归法 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        if ($b == 0) {
            return $a;
        }
        return $this->add($a ^ $b, ($a & $b) << 1);
    }
    // 迭代法
    // function add($a, $b) {
    // while ($b != 0) {
    // $temp = $a ^ $b;
    // $b = ($a & $b) << 1;
    // $a = $temp;
    // }
    // return $a;
    // }}

Problem-solving ideas 3 - Bit by bit calculation

The principle is equivalent to bit operation, but the implementation of manual bit operation

Code

class Solution {
    /** 
    * 此方法暂时只支持两个正数相加 
    * @param Integer $a 
    * @param Integer $b 
    * @return Integer 
    */
    function add($a, $b) {
        $a = "$a";
        $b = "$b";
        $lenA = strlen($a);
        $lenB = strlen($b);
        $res = &#39;&#39;;
        $flag = 0;   // 进位标志
        for ($i = $lenA - 1, $j = $lenB - 1; $i >= 0 || $j >= 0; $i--, $j--) {
            // 超出的位数用 0 表示
            $itemA = ($i >= 0) ? $a[$i] : 0;
            $itemB = ($j >= 0) ? $b[$j] : 0;
            // 求位数和
            $sum = (int)$itemA + (int)$itemB + $flag;
            // 是否进位
            if ($sum >= 10) {
                $flag = 1;  // 只可能为1
                $sum = $sum - 10;
            } else {
                $flag = 0;
            }
            
            $res = $sum . $res;
        }
        // 处理最高位进位
        return $flag > 0 ? $flag . $res : $res;
    }}

Recommended learning: php video tutorial

The above is the detailed content of How to implement addition in PHP without using addition, subtraction, multiplication and division symbols. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:hxd.life. If there is any infringement, please contact admin@php.cn delete