📜  计算具有偶数和的整数对

📅  最后修改于: 2021-10-26 07:01:32             🧑  作者: Mango

给定两个整数NM ,任务是计算所有可能的整数对(i, j) (1 ≤ i ≤ N, 1 ≤ j ≤ M),使得i + j是偶数。

例子:

朴素的方法:解决这个问题的最简单的方法是遍历范围[1, M]中的每个数字,遍历范围[1, N]并生成所有可能的对。对于每个可能的对,检查其总和是否为偶数。如果发现为真,则增加计数。最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count pairs with even sum
int countEvenPairs(int N, int M)
{
     
    // Stores the count of pairs
    // with even sum
    int count = 0;
 
    // Traverse the range 1 to N
    for(int i = 1; i <= N; i++)
    {
         
        // Traverse the range 1 to M
        for(int j = 1; j <= M; j++)
        {
             
            // Check if the sum of the
            // pair (i, j) is even or not
            if ((i + j) % 2 == 0)
            {
                 
                // Update count
                count++;
            }
        }
    }
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
    int M = 6;
     
    cout << countEvenPairs(N, M) << endl;
     
    return 0;
}
 
// This code is contributed by akhilsaini


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to count pairs with even sum
    public static int countEvenPairs(
        int N, int M)
    {
 
        // Stores the count of pairs
        // with even sum
        int count = 0;
 
        // Traverse the range 1 to N
        for (int i = 1; i <= N; i++) {
 
            // Traverse the range 1 to M
            for (int j = 1; j <= M; j++) {
 
                // Check if the sum of the
                // pair (i, j) is even or not
                if ((i + j) % 2 == 0) {
 
                    // Update count
                    count++;
                }
            }
        }
 
        // Return the count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
        int M = 6;
        System.out.print(
            countEvenPairs(N, M));
    }
}


Python3
# Python3 program for the above approach
 
# Function to count pairs with even sum
def countEvenPairs(N, M):
     
    # Stores the count of pairs
    # with even sum
    count = 0
     
    # Traverse the range 1 to N
    for i in range(1, N + 1):
     
        # Traverse the range 1 to M
        for j in range(1, M + 1):
         
            # Check if the sum of the
            # pair (i, j) is even or not
            if ((i + j) % 2 == 0):
               
                # Update count
                count += 1
     
    # Return the count
    return count
 
# Driver Code
if __name__ == '__main__':
     
  N = 4
  M = 6
   
  print(countEvenPairs(N, M))
   
# This code is contributed by akhilsaini


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to count pairs with even sum
public static int countEvenPairs(int N, int M)
{
     
    // Stores the count of pairs
    // with even sum
    int count = 0;
 
    // Traverse the range 1 to N
    for(int i = 1; i <= N; i++)
    {
         
        // Traverse the range 1 to M
        for(int j = 1; j <= M; j++)
        {
             
            // Check if the sum of the
            // pair (i, j) is even or not
            if ((i + j) % 2 == 0)
            {
                 
                // Update count
                count++;
            }
        }
    }
     
    // Return the count
    return count;
}
 
// Driver Code
public static void Main()
{
    int N = 4;
    int M = 6;
     
    Console.WriteLine(
        countEvenPairs(N, M));
}
}
 
// This code is contributed by akhilsaini


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count even pairs
int countEvenPairs(int N, int M)
{
 
    // Stores count of pairs having even sum
    int count = 0;
 
    // Stores count of even numbers up to N
    int nEven = floor(N / 2);
 
    // Stores count of odd numbers up to N
    int nOdd = ceil(N / 2);
 
    // Stores count of even numbers up to M
    int mEven = floor(M / 2);
 
    // Stores count of odd numbers up to M
    int mOdd = ceil(M / 2);
    count = nEven * mEven + nOdd * mOdd;
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
    int M = 6;
    cout << countEvenPairs(N, M);
    return 0;
}
 
// This code is contributed by Dharanendra L V


Java
// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to count even pairs
    public static int countEvenPairs(
        int N, int M)
    {
 
        // Stores count of pairs having even sum
        int count = 0;
 
        // Stores count of even numbers up to N
        int nEven = (int)Math.floor((double)N / 2);
 
        // Stores count of odd numbers up to N
        int nOdd = (int)Math.ceil((double)N / 2);
 
        // Stores count of even numbers up to M
        int mEven = (int)Math.floor((double)M / 2);
 
        // Stores count of odd numbers up to M
        int mOdd = (int)Math.ceil((double)M / 2);
 
        count = nEven * mEven + nOdd * mOdd;
 
        // Return the count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
        int M = 6;
        System.out.print(countEvenPairs(N, M));
    }
}


Python3
# Python3 program for the above approach
import math
 
# Function to count even pairs
def countEvenPairs(N, M):
   
    # Stores count of pairs having even sum
    count = 0;
 
    # Stores count of even numbers up to N
    nEven = int(math.floor(N / 2));
 
    # Stores count of odd numbers up to N
    nOdd = int(math.ceil(N / 2));
 
    # Stores count of even numbers up to M
    mEven = int(math.floor(M / 2));
 
    # Stores count of odd numbers up to M
    mOdd = int(math.ceil(M / 2));
 
    count = nEven * mEven + nOdd * mOdd;
 
    # Return the count
    return count;
 
# Driver Code
if __name__ == '__main__':
    N = 4;
    M = 6;
    print(countEvenPairs(N, M));
 
# This code is contributed by 29AjayKumar


C#
// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to count even pairs
  public static int countEvenPairs(int N, int M)
  {
 
    // Stores count of pairs having even sum
    int count = 0;
 
    // Stores count of even numbers up to N
    int nEven = (int)Math.Floor((double)N / 2);
 
    // Stores count of odd numbers up to N
    int nOdd = (int)Math.Ceiling((double)N / 2);
 
    // Stores count of even numbers up to M
    int mEven = (int)Math.Floor((double)M / 2);
 
    // Stores count of odd numbers up to M
    int mOdd = (int)Math.Ceiling((double)M / 2);
    count = nEven * mEven + nOdd * mOdd;
 
    // Return the count
    return count;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 4;
    int M = 6;
    Console.Write(countEvenPairs(N, M));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出:
12

时间复杂度: O(N 2 )
空间复杂度: O(1)

高效的方法:上述方法可以基于以下观察进行优化:

请按照以下步骤解决问题:

  • 初始化两个变量,比如nEvennOdd ,以存储最多N的奇数和偶数整数。
  • 初始化两个变量,比如mEvenmOdd ,以存储最多M的偶数和奇数整数。
  • 最后,使用公式计算所需的对数:

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to count even pairs
int countEvenPairs(int N, int M)
{
 
    // Stores count of pairs having even sum
    int count = 0;
 
    // Stores count of even numbers up to N
    int nEven = floor(N / 2);
 
    // Stores count of odd numbers up to N
    int nOdd = ceil(N / 2);
 
    // Stores count of even numbers up to M
    int mEven = floor(M / 2);
 
    // Stores count of odd numbers up to M
    int mOdd = ceil(M / 2);
    count = nEven * mEven + nOdd * mOdd;
 
    // Return the count
    return count;
}
 
// Driver Code
int main()
{
    int N = 4;
    int M = 6;
    cout << countEvenPairs(N, M);
    return 0;
}
 
// This code is contributed by Dharanendra L V

Java

// Java program for the above approach
import java.io.*;
 
class GFG {
 
    // Function to count even pairs
    public static int countEvenPairs(
        int N, int M)
    {
 
        // Stores count of pairs having even sum
        int count = 0;
 
        // Stores count of even numbers up to N
        int nEven = (int)Math.floor((double)N / 2);
 
        // Stores count of odd numbers up to N
        int nOdd = (int)Math.ceil((double)N / 2);
 
        // Stores count of even numbers up to M
        int mEven = (int)Math.floor((double)M / 2);
 
        // Stores count of odd numbers up to M
        int mOdd = (int)Math.ceil((double)M / 2);
 
        count = nEven * mEven + nOdd * mOdd;
 
        // Return the count
        return count;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 4;
        int M = 6;
        System.out.print(countEvenPairs(N, M));
    }
}

蟒蛇3

# Python3 program for the above approach
import math
 
# Function to count even pairs
def countEvenPairs(N, M):
   
    # Stores count of pairs having even sum
    count = 0;
 
    # Stores count of even numbers up to N
    nEven = int(math.floor(N / 2));
 
    # Stores count of odd numbers up to N
    nOdd = int(math.ceil(N / 2));
 
    # Stores count of even numbers up to M
    mEven = int(math.floor(M / 2));
 
    # Stores count of odd numbers up to M
    mOdd = int(math.ceil(M / 2));
 
    count = nEven * mEven + nOdd * mOdd;
 
    # Return the count
    return count;
 
# Driver Code
if __name__ == '__main__':
    N = 4;
    M = 6;
    print(countEvenPairs(N, M));
 
# This code is contributed by 29AjayKumar

C#

// C# program for the above approach
using System;
 
class GFG
{
 
  // Function to count even pairs
  public static int countEvenPairs(int N, int M)
  {
 
    // Stores count of pairs having even sum
    int count = 0;
 
    // Stores count of even numbers up to N
    int nEven = (int)Math.Floor((double)N / 2);
 
    // Stores count of odd numbers up to N
    int nOdd = (int)Math.Ceiling((double)N / 2);
 
    // Stores count of even numbers up to M
    int mEven = (int)Math.Floor((double)M / 2);
 
    // Stores count of odd numbers up to M
    int mOdd = (int)Math.Ceiling((double)M / 2);
    count = nEven * mEven + nOdd * mOdd;
 
    // Return the count
    return count;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 4;
    int M = 6;
    Console.Write(countEvenPairs(N, M));
  }
}
 
// This code is contributed by shikhasingrajput

Javascript


输出:
12

时间复杂度: O(1)
辅助空间: O(1)