📜  使用给定的N个数字对给定的数学表达式进行奇偶校验

📅  最后修改于: 2021-05-04 12:20:25             🧑  作者: Mango

给定N个正整数A 1 ,A 2 ,…,A N ,任务是确定表达式S的奇偶性。
对于给定的N个数字,表达式S给出为:
S = 1 + \sum_{i=1}^{N}A_i + \sum_{i=1}^{N}(A_i * A_{i+1}) + \sum_{i=1}^{N}(A_i * A_{i+1} * A_{i+2}) + ...+ (A_1 * A_2 * A_3 * ... * A_N)

例子:

幼稚的方法:针对此问题的幼稚的方法是在给定表达式中插入A i的所有值,并找到给定表达式的奇偶校验。此方法不适用于较高的N值,因为对于高阶数字而言,乘法不是一个常数运算。而且,该值可能会变得太大以至于可能导致整数溢出。

高效的方法:想法是对表达式执行一些处理,并将表达式简化为更简单的术语,以便可以在不计算值的情况下检查奇偶校验。令N =3。然后:

  1. 表达式S为:
  2. 现在,将相同的表达式重构为如下形式:
  3. 从上述等式中取(1 + A 1 )共通,
  4. 最后,在采用(1 + A 2 )通用的情况下,最终表达式变为:
  5. 通过对称,对于N个元素,表达式S变为:
  6. 显然,要使数字成为偶数,答案必须是偶数。众所周知,答案是偶数。
  7. 因此,其思想是检查给定输入中的任何数字是否为奇数。如果是,则在加一时,它变为偶数,并且值是偶数奇偶校验。

下面是上述方法的实现:

C++
// C++ program to determine the
// parity of the given mathematical
// expression
  
#include 
using namespace std;
  
void getParity(
    int n,
    const vector& A)
{
  
    // Iterating through the
    // given integers
    for (auto x : A) {
        if (x & 1) {
  
            // If any odd number
            // is present, then S
            // is even parity
            cout << "Even" << endl;
            return;
        }
    }
  
    // Else, S is odd parity
    cout << "Odd" << endl;
}
  
// Driver code
int main()
{
  
    int N = 3;
    vector A = { 2, 3, 1 };
    getParity(N, A);
  
    return 0;
}


Java
// Java program to determine the 
// parity of the given mathematical 
// expression 
class GFG{
  
static void getParity(int n, int []A) 
{ 
  
    // Iterating through the 
    // given integers 
    for (int x : A) 
    { 
        if ((x & 1) == 1) 
        { 
  
            // If any odd number 
            // is present, then S 
            // is even parity 
            System.out.println("Even"); 
            return; 
        } 
    } 
  
    // Else, S is odd parity 
    System.out.println("Odd"); 
} 
  
// Driver code 
public static void main(String[] args)
{ 
    int N = 3; 
    int [] A = { 2, 3, 1 }; 
    getParity(N, A); 
}
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 program to determine the
# parity of the given mathematical
# expression
def getParity(n, A):
  
    # Iterating through 
    # the given integers
    for x in A:
        if (x & 1):
  
            # If any odd number
            # is present, then S
            # is even parity
            print("Even")
            return
  
    # Else, S is odd parity
    print("Odd")
      
# Driver code
if __name__ == '__main__':
  
    N = 3
    A = [ 2, 3, 1 ]
      
    getParity(N, A)
  
# This code is contributed by mohit kumar 29


C#
// C# program to determine the 
// parity of the given mathematical 
// expression 
using System;
  
public class GFG{
  
    static void getParity(int n, int []A) 
    { 
      
        // Iterating through the 
        // given integers 
        foreach (int x in A) 
        { 
            if ((x & 1) == 1) 
            { 
      
                // If any odd number 
                // is present, then S 
                // is even parity 
                Console.WriteLine("Even"); 
                return; 
            } 
        } 
      
        // Else, S is odd parity 
        Console.WriteLine("Odd"); 
    } 
      
    // Driver code 
    public static void Main(string[] args)
    { 
        int N = 3; 
        int [] A = { 2, 3, 1 }; 
        getParity(N, A); 
    }
}
  
// This code is contributed by AnkitRai01


输出:
Even

时间复杂度: O(N) ,其中N是给定数字的数量。