给定两个整数N和M ,任务是对所有可能的整数对(i,j) (1≤i≤N ,1≤j≤M)进行计数,以使i + j为偶数。
例子:
Input: N = 6, M = 4
Output: 12
Explanation: The pairs (1, 1), (1, 3), (2, 2), (2, 4), (3, 1), (3, 3), (4, 2), (4, 4), (5, 1), (5, 3), (6, 2), (6, 4) satisfy the required condition. Therefore, the count is 12.
Input: N = 2 and M = 8
Output: 8
天真的方法:解决此问题的最简单方法是遍历该范围内每个数字的范围[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)
高效的方法:可以基于以下观察来优化上述方法:
- Even number + Even number = Even number
- Odd number + Odd number = Even number
请按照以下步骤解决问题:
- 初始化两个变量,例如nEven和nOdd ,以存储直到N的奇数和偶数整数的计数。
- 初始化两个变量,例如mEven和mOdd ,以存储偶数和奇数整数直到M的计数。
- 最后,使用公式计算所需的对数:
count = nEven * mEven + nOdd * mOdd
下面是上述方法的实现:
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
Java脚本
输出:
12
时间复杂度: O(1)
辅助空间: O(1)