📜  仅使用+算术运算运算符实现*,–和/操作

📅  最后修改于: 2021-05-04 17:16:03             🧑  作者: Mango

给定两个数字,仅使用“ +”算术运算运算符对其执行乘法,减法和除法运算。

可以执行以下操作:

Subtraction :-  a - b = a + (-1)*b.
Multiplication :- a * b = a + a + a ... b times.
Division :- a / b =  continuously subtract b from a and 
                  count how many times we can do that.

上面的步骤看起来很简单,但是有些挑战,因为我们甚至不能使用–减去。

C++
// CPP code to illustrate *, -, / using only
// '+' arithmatic operator
#include 
using namespace std;
  
// Function to flip the sign using only "+"
// operator (It is simple with '*' allowed.
// We need to do a = (-1)*a
int flipSign(int a)
{
    int neg = 0;
  
    // If sign is + ve turn it -ve
    // and vice-versa
    int tmp = a < 0 ? 1 : -1;
    while (a != 0)
    {
        neg += tmp;
        a += tmp;
    }
    return neg;
}
  
// Check if a and b are of different signs
bool areDifferentSign(int a, int b)
{
    return ((a<0 && b> 0) || (a > 0 && b < 0));
}
  
// Function to subtract two numbers
// by negating b and adding them
int sub(int a, int b)
{
    // Negating b
    return a + flipSign(b);
}
  
// Function to multiply a by b by
// adding a to itself b times
int mul(int a, int b)
{
    // because algo is faster if b 0; i--)
        sum += a;
  
    // Check if final sign must be -ve or + ve
    if (b < 0)
        sum = flipSign(sum);
  
    return sum;
}
  
// Function to divide a by b by counting how many
// times 'b' can be subtracted from 'a' before
// getting 0
int division(int a, int b)
{
    // Raise exception if b is 0
    if (b == 0)
        throw(b);
  
    int quotient = 0, dividend;
  
    // Negating b to subtract from a
    int divisor = flipSign(abs(b));
  
    // Subtracting divisor from dividend
    for (dividend = abs(a); dividend >= abs(divisor);
                                dividend += divisor)
        quotient++;
  
    // Check if a and b are of similar symbols or not
    if (areDifferentSign(a, b))
        quotient = flipSign(quotient);
    return quotient;
}
  
// Driver code
int main()
{
    cout << "Subtraction is " << sub(4, -2) << endl;
    cout << "Product is " << mul(-9, 6) << endl;
  
    try
    {
        cout << "Division is " << division(8, 2);
    }
  
    catch (int k)
    {
        cout << " Exception :- Divide by 0";
    }
    return 0;
}


Java
// Java code to illustrate *, -, / using only 
// '+' arithmatic operator 
  
class GFG{
      
// Function to flip the sign using only "+" 
// operator (It is simple with '*' allowed. 
// We need to do a = (-1)*a 
static int flipSign(int a) 
{ 
    int neg = 0; 
  
    // If sign is + ve turn it -ve 
    // and vice-versa 
    int tmp = a < 0 ? 1 : -1; 
    while (a != 0) 
    { 
        neg += tmp; 
        a += tmp; 
    } 
    return neg; 
} 
  
// Check if a and b are of different signs 
static boolean areDifferentSign(int a, int b) 
{ 
    return ((a < 0 && b > 0) || (a > 0 && b < 0)); 
} 
  
// Function to subtract two numbers 
// by negating b and adding them 
static int sub(int a, int b) 
{ 
    // Negating b 
    return a + flipSign(b); 
} 
  
// Function to multiply a by b by 
// adding a to itself b times 
static int mul(int a, int b) 
{ 
    // because algo is faster if b 0; i--) 
        sum += a; 
  
    // Check if final sign must be -ve or + ve 
    if (b < 0) 
        sum = flipSign(sum); 
  
    return sum; 
} 
  
// Function to divide a by b by counting  
// how many times 'b' can be subtracted  
// from 'a' before getting 0 
static int division(int a, int b) 
{ 
    // Raise exception if b is 0 
    if (b == 0) 
        throw new ArithmeticException(); 
  
    int quotient = 0, dividend; 
  
    // Negating b to subtract from a 
    int divisor = flipSign(Math.abs(b)); 
  
    // Subtracting divisor from dividend 
    for (dividend = Math.abs(a); dividend >= Math.abs(divisor); 
         dividend += divisor) 
        quotient++; 
  
    // Check if a and b are of similar symbols or not 
    if (areDifferentSign(a, b)) 
        quotient = flipSign(quotient); 
    return quotient; 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    System.out.println("Subtraction is " + sub(4, -2)); 
    System.out.println("Product is " + mul(-9, 6)); 
  
    try
    { 
        System.out.println("Division is " + division(8, 2)); 
    } 
  
    catch (ArithmeticException e) 
    { 
        System.out.println("Exception :- Divide by 0"); 
    } 
} 
}
  
// This code is contributed by mits


Python3
# Python3 code to illustrate *, -, / using 
# only  '+' arithmatic operator 
  
# Function to flip the sign using only "+" 
# operator (It is simple with '*' allowed. 
# We need to do a = (-1)*a 
def flipSign(a): 
  
    neg = 0; 
  
    # If sign is + ve turn it -ve 
    # and vice-versa 
    tmp = 1 if a < 0 else -1; 
    while (a != 0): 
        neg += tmp; 
        a += tmp; 
  
    return neg; 
  
# Check if a and b are of different signs 
def areDifferentSign(a, b):
    return ((a < 0 and b > 0) or 
            (a > 0 and b < 0)); 
  
# Function to subtract two numbers 
# by negating b and adding them 
def sub(a, b): 
  
    # Negating b 
    return a + flipSign(b); 
  
# Function to multiply a by b by 
# adding a to itself b times 
def mul(a, b): 
  
    # because algo is faster if b


C#
// C# code to illustrate *, -, / using only 
// '+' arithmatic operator 
using System;
class GFG
{
// Function to flip the sign using only "+" 
// operator (It is simple with '*' allowed. 
// We need to do a = (-1)*a 
static int flipSign(int a) 
{ 
    int neg = 0; 
  
    // If sign is + ve turn it -ve 
    // and vice-versa 
    int tmp = a < 0 ? 1 : -1; 
    while (a != 0) 
    { 
        neg += tmp; 
        a += tmp; 
    } 
    return neg; 
} 
  
// Check if a and b are of different signs 
static bool areDifferentSign(int a, int b) 
{ 
    return ((a < 0 && b > 0) || (a > 0 && b < 0)); 
} 
  
// Function to subtract two numbers 
// by negating b and adding them 
static int sub(int a, int b) 
{ 
    // Negating b 
    return a + flipSign(b); 
} 
  
// Function to multiply a by b by 
// adding a to itself b times 
static int mul(int a, int b) 
{ 
    // because algo is faster if b 0; i--) 
        sum += a; 
  
    // Check if final sign must be -ve or + ve 
    if (b < 0) 
        sum = flipSign(sum); 
  
    return sum; 
} 
  
// Function to divide a by b by counting how many 
// times 'b' can be subtracted from 'a' before 
// getting 0 
static int division(int a, int b) 
{ 
    // Raise exception if b is 0 
    if (b == 0) 
        throw new ArithmeticException(); 
  
    int quotient = 0, dividend; 
  
    // Negating b to subtract from a 
    int divisor = flipSign(Math.Abs(b)); 
  
    // Subtracting divisor from dividend 
    for (dividend = Math.Abs(a); dividend >= Math.Abs(divisor); 
                                dividend += divisor) 
        quotient++; 
  
    // Check if a and b are of similar symbols or not 
    if (areDifferentSign(a, b)) 
        quotient = flipSign(quotient); 
    return quotient; 
} 
  
// Driver code 
public static void Main() 
{ 
    Console.WriteLine("Subtraction is " + sub(4, -2)); 
    Console.WriteLine("Product is " + mul(-9, 6)); 
    try
    { 
        Console.WriteLine("Division is " + division(8, 2)); 
    } 
    catch (Exception) 
    { 
        Console.WriteLine("Exception :- Divide by 0"); 
    } 
} 
}
  
//This code is contributed by mits


PHP
 0) || 
            ($a > 0 && $b < 0)); 
} 
  
// Function to subtract two numbers 
// by negating b and adding them 
function sub($a, $b) 
{ 
    // Negating b 
    return $a + flipSign($b); 
} 
  
// Function to multiply a by b by 
// adding a to itself b times 
function mul($a, $b) 
{ 
    // because algo is faster if b 0; $i--) 
        $sum += $a; 
  
    // Check if final sign must be 
    // -ve or + ve 
    if ($b < 0) 
        $sum = flipSign($sum); 
  
    return $sum; 
} 
  
// Function to divide a by b by counting 
// how many times 'b' can be subtracted 
// from 'a' before getting 0 
function division($a, $b) 
{
    $quotient = 0; 
  
    // Negating b to subtract from a 
    $divisor = flipSign(abs($b)); 
  
    // Subtracting divisor from dividend 
    for ($dividend = abs($a);
         $dividend >= abs($divisor); 
         $dividend += $divisor) 
        $quotient++; 
  
    // Check if a and b are of similar
    // symbols or not 
    if (areDifferentSign($a, $b)) 
        $quotient = flipSign($quotient); 
    return $quotient; 
} 
  
// Driver code 
print("Subtraction is " . sub(4, -2) . "\n"); 
print("Product is " . mul(-9, 6) . "\n");
list($a, $b) = array(8, 2);
if($b)
    print("Division is " . division($a, $b));
else
    print("Exception :- Divide by 0"); 
  
// This code is contributed by mits
?>


输出:

Subtraction is 6
Product is -54
Division is 4

相关文章 :

  • 不使用算术运算运算符将两个数字相加
  • 减去两个数字而不使用算术运算运算符
  • 将两个整数相乘而不使用乘法,除法和按位运算运算符,并且不使用循环