📌  相关文章
📜  检查从a到b的整数的乘积是正,负还是零

📅  最后修改于: 2021-04-21 21:09:40             🧑  作者: Mango

给定两个整数ab ,任务是检查rage v [a,b]中的整数乘积,即a *(a + 1)*(a + 2)*…* b是正,负还是零。 。

例子:

原始的方法:我们可以运行一个循环,B和乘法开始到B和检查该产品是否为正负或零的数字。对于ab的较大值,此解决方案将失败,并会导致溢出。

高效的方法:可能有以下三种情况:

  1. 如果a> 0b> 0,则所得乘积将为正。
  2. 如果a <0b> 0,则结果将为零,因为a *(a + 1)*…* 0 *…(b – 1)* b = 0
  3. 如果a <0b <0,则结果将取决于数字的计数(因为所有数字均为负数)
    • 如果负数的计数为偶数,则结果将为正。
    • 否则结果将为负。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to check whether the product
// of integers of the range [a, b]
// is positive, negative or zero
void solve(long long int a, long long int b)
{
  
    // If both a and b are positive then
    // the product will be positive
    if (a > 0 && b > 0) {
        cout << "Positive";
    }
  
    // If a is negative and b is positive then
    // the product will be zero
    else if (a <= 0 && b >= 0) {
        cout << "Zero" << endl;
    }
  
    // If both a and b are negative then
    // we have to find the count of integers
    // in the range
    else {
  
        // Total integers in the range
        long long int n = abs(a - b) + 1;
  
        // If n is even then the resultant
        // product is positive
        if (n % 2 == 0) {
            cout << "Positive" << endl;
        }
        // If n is odd then the resultant
        // product is negative
        else {
            cout << "Negative" << endl;
        }
    }
}
  
// Driver code
int main()
{
    int a = -10, b = -2;
  
    solve(a, b);
  
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
  
class GFG 
{
  
// Function to check whether the product
// of integers of the range [a, b]
// is positive, negative or zero
static void solve(long a, long b)
{
  
    // If both a and b are positive then
    // the product will be positive
    if (a > 0 && b > 0) 
    {
        System.out.println( "Positive");
    }
  
    // If a is negative and b is positive then
    // the product will be zero
    else if (a <= 0 && b >= 0)
    {
        System.out.println( "Zero" );
    }
  
    // If both a and b are negative then
    // we have to find the count of integers
    // in the range
    else 
    {
  
        // Total integers in the range
        long n = Math.abs(a - b) + 1;
  
        // If n is even then the resultant
        // product is positive
        if (n % 2 == 0) 
        {
            System.out.println( "Positive");
        }
          
        // If n is odd then the resultant
        // product is negative
        else
        {
            System.out.println( "Negative");
        }
    }
}
  
    // Driver code
    public static void main (String[] args) 
    {
        int a = -10, b = -2;
      
        solve(a, b);
    }
}
  
// This code is contributed by anuj_67..


Python3
# Python 3 implementation of the approach
  
# Function to check whether the product
# of integers of the range [a, b]
# is positive, negative or zero
def solve(a,b):
      
    # If both a and b are positive then
    # the product will be positive
    if (a > 0 and b > 0):
        print("Positive")
  
    # If a is negative and b is positive then
    # the product will be zero
    elif (a <= 0 and b >= 0):
        print("Zero")
  
    # If both a and b are negative then
    # we have to find the count of integers
    # in the range
    else:
          
        # Total integers in the range
        n = abs(a - b) + 1
  
        # If n is even then the resultant
        # product is positive
        if (n % 2 == 0):
            print("Positive")
              
        # If n is odd then the resultant
        # product is negative
        else:
            print("Negative")
  
# Driver code
if __name__ == '__main__':
    a = -10
    b = -2
  
    solve(a, b)
      
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    // Function to check whether the product 
    // of integers of the range [a, b] 
    // is positive, negative or zero 
    static void solve(long a, long b) 
    { 
      
        // If both a and b are positive then 
        // the product will be positive 
        if (a > 0 && b > 0) 
        { 
            Console.WriteLine( "Positive"); 
        } 
      
        // If a is negative and b is positive then 
        // the product will be zero 
        else if (a <= 0 && b >= 0) 
        { 
            Console.WriteLine( "Zero" ); 
        } 
      
        // If both a and b are negative then 
        // we have to find the count of integers 
        // in the range 
        else
        { 
      
            // Total integers in the range 
            long n = Math.Abs(a - b) + 1; 
      
            // If n is even then the resultant 
            // product is positive 
            if (n % 2 == 0) 
            { 
                Console.WriteLine( "Positive"); 
            } 
              
            // If n is odd then the resultant 
            // product is negative 
            else
            { 
                Console.WriteLine( "Negative"); 
            } 
        } 
    } 
      
    // Driver code 
    public static void Main () 
    { 
        int a = -10, b = -2; 
      
        solve(a, b); 
    }
}
  
// This code is contributed by AnkitRai01


输出:
Negative