📌  相关文章
📜  检查“ n”个数字的乘积是偶数还是奇数

📅  最后修改于: 2021-04-22 10:09:17             🧑  作者: Mango

给定一个包含n个数字的数组arr [] 。问题是检查给定n个数字的乘积是偶数还是奇数。
例子:

Input : arr[] = {2, 4, 3, 5}
Output : Even
Product = 2 * 4 * 3 * 5 = 120

Input : arr[] = {3, 9, 7, 1}
Output : Odd

一个简单的解决方案是先找到产品,然后检查产品是偶数还是奇数。此解决方案导致大型阵列溢出。
更好的解决方案基于以下数学计算事实:

  1. 两个偶数的乘积是偶数。
  2. 两个奇数的乘积是奇数。
  3. 一个偶数和一个奇数的乘积是偶数。

基于上述事实,如果出现单个偶数,则n个数字的整个乘积将为奇数。

C++
// C++ implementation to check whether product of
// 'n' numbers is even or odd
#include 
 
using namespace std;
 
// function to check whether product of
// 'n' numbers is even or odd
bool isProductEven(int arr[], int n)
{
    for (int i = 0; i < n; i++)
 
        // if a single even number is found, then
        // final product will be an even number
        if ((arr[i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 2, 4, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isProductEven(arr, n))
        cout << "Even";
    else
        cout << "Odd";
    return 0;
}


Java
// Java implementation to check whether product of
// 'n' numbers is even or odd
 
public class GFG {
     
    // function to check whether product of
    // 'n' numbers is even or odd
    static boolean isProductEven(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
 
            // if a single even number is found, then
            // final product will be an even number
            if ((arr[i] & 1) == 0)
                return true;
 
        // product is an odd number
        return false;
    }
 
 
    // Driver code
    public static void main(String args[])
    {
            int arr[] = { 2, 4, 3, 5 };
            int n = arr.length ;
            if (isProductEven(arr, n))
                System.out.println("Even");
            else
                System.out.println("Odd") ;     
    }
    // This Code is contributed by ANKITRAI1
}


Python3
# Python3 implementation to
# check whether product of 'n'
# numbers is even or odd
 
# function to check whether
# product of 'n' numbers is
# even or odd
def isProductEven(arr, n):
 
    for i in range(0, n):
 
        # if a single even number is
        # found, then final product
        # will be an even number
        if ((arr[i] & 1) == 0):
            return True
 
    # product is an odd number
    return False
 
# Driver Code
arr = [ 2, 4, 3, 5 ]
n = len(arr)
if (isProductEven(arr, n)):
    print("Even")
else:
    print("Odd")
 
# This code is contributed
# by ihritik


C#
// C# implementation to check
// whether product of 'n'
// numbers is even or odd
using System;
 
class GFG
{
     
// function to check whether
// product of 'n' numbers
// is even or odd
static bool isProductEven(int []arr, int n)
{
    for (int i = 0; i < n; i++)
 
        // if a single even number is
        // found, then final product
        // will be an even number
        if ((arr[i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 4, 3, 5 };
    int n = arr.Length;
    if (isProductEven(arr, n))
        Console.WriteLine("Even");
    else
        Console.WriteLine("Odd") ;
}
}
 
// This code is contributed by ihritik


PHP


Javascript


输出:

Even

时间复杂度: O(n)。