给定两个正整数X和Y ,任务是通过从1到X范围内的任意整数和从1到Y范围内的另一个整数对对进行偶数加总计数。
例子:
Input : X = 2, Y = 3
Output : 3
Explanation : All such possible pairs are {1, 1}, {1, 3}, {2, 2}.
方法:请按照以下步骤解决问题:
- 初始化一个变量,例如cntXEvenNums ,以存储通过将X除以2获得的[1,X]范围内的偶数计数。
- 初始化一个变量,例如cntXOddNums ,以存储通过将(X +1)除以2得到的[1,X]范围内的奇数计数。
- 初始化一个变量,例如cntYEvenNums ,通过将Y除以2来存储1到Y之间的偶数计数。
- 初始化一个变量,例如cntYOddNums ,通过将(Y +1)除以2来存储1到Y之间的奇数计数。
- 初始化变量,说cntPairs,用cntYEvenNums与cntYOddNums乘以cntXEvenNums和乘以cntXOddNums存储甚至和对的数量,发现两者的总和。
- 最后,打印获得的cntPairs的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to count even
// sum pairs in the given range
long long cntEvenSumPairs(long long X, long long Y)
{
// Stores the count of even
// numbers between 1 to X
long long cntXEvenNums = X / 2;
// Stores the count of odd
// numbers between 1 to X
long long cntXOddNums = (X + 1) / 2;
// Stores the count of even
// numbers between 1 to Y
long long cntYEvenNums = Y / 2;
// Stores the count of odd
// numbers between 1 to Y
long long cntYOddNums = (Y + 1) / 2;
// Stores the count of
// pairs having even sum
long long cntPairs
= (cntXEvenNums * 1LL * cntYEvenNums)
+ (cntXOddNums * 1LL * cntYOddNums);
// Retuens the count of pairs
// having even sum
return cntPairs;
}
// Driver Code
int main()
{
long long X = 2;
long long Y = 3;
cout << cntEvenSumPairs(X, Y);
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG {
// Function to count maximum even
// sum pairs in the given range
static long cntEvenSumPairs(long X, long Y)
{
// Stores the count of even
// numbers between 1 to X
long cntXEvenNums = X / 2;
// Stores the count of odd
// numbers between 1 to X
long cntXOddNums = (X + 1) / 2;
// Stores the count of even
// numbers between 1 to Y
long cntYEvenNums = Y / 2;
// Stores the count of odd
// numbers between 1 to Y
long cntYOddNums = (Y + 1) / 2;
// Stores the count of
// pairs having even sum
long cntPairs = (cntXEvenNums * cntYEvenNums)
+ (cntXOddNums * cntYOddNums);
// Retuens the count of pairs
// having even sum
return cntPairs;
}
// Driver Code
public static void main(String[] args)
{
long X = 2;
long Y = 3;
System.out.println(cntEvenSumPairs(X, Y));
}
}
Python
# Python program to implement
# the above approach
# Function to count even
# sum pairs in the given range
def cntEvenSumPairs(X, Y):
# Stores the count of even
# numbers between 1 to X
cntXEvenNums = X / 2
# Stores the count of odd
# numbers between 1 to X
cntXOddNums = (X + 1) / 2
# Stores the count of even
# numbers between 1 to Y
cntYEvenNums = Y / 2
# Stores the count of odd
# numbers between 1 to Y
cntYOddNums = (Y + 1) / 2
# Stores the count of
# pairs having even sum
cntPairs = ((cntXEvenNums * cntYEvenNums) +
(cntXOddNums * cntYOddNums))
# Returns the count of pairs
# having even sum
return cntPairs
# Driver code
X = 2
Y = 3
print(cntEvenSumPairs(X, Y))
# This code is contributed by hemanth gadarla
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to count maximum even
// sum pairs in the given range
static long cntEvenSumPairs(long X, long Y)
{
// Stores the count of even
// numbers between 1 to X
long cntXEvenNums = X / 2;
// Stores the count of odd
// numbers between 1 to X
long cntXOddNums = (X + 1) / 2;
// Stores the count of even
// numbers between 1 to Y
long cntYEvenNums = Y / 2;
// Stores the count of odd
// numbers between 1 to Y
long cntYOddNums = (Y + 1) / 2;
// Stores the count of
// pairs having even sum
long cntPairs = (cntXEvenNums * cntYEvenNums) +
(cntXOddNums * cntYOddNums);
// Retuens the count of pairs
// having even sum
return cntPairs;
}
// Driver Code
public static void Main(string[] args)
{
long X = 2;
long Y = 3;
Console.WriteLine(cntEvenSumPairs(X, Y));
}
}
// This code is contributed by chitranayal
Javascript
输出:
3
时间复杂度:O(1)
辅助空间:O(1)