📌  相关文章
📜  通过分别从两个给定范围中选择两个整数来计算可能的偶数和对

📅  最后修改于: 2021-10-26 06:46:13             🧑  作者: Mango

给定两个正整数XY ,任务是通过选择范围1X 中的任何整数和范围1Y 中的另一个整数来计算具有偶数和可能的对。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如cntXEvenNums ,以存储通过将X除以2获得的范围[1, X]中偶数的计数。
  • 初始化一个变量,比如cntXOddNums ,以存储通过将(X + 1)除以2获得的范围[1, X] 中的奇数计数。
  • 初始化一个变量,比如cntYEvenNums ,通过将Y除以2来存储1Y之间偶数的计数。
  • 初始化一个变量,比如cntYOddNums ,通过将(Y + 1)除以2来存储1Y之间的奇数计数。
  • 初始化变量,说cntPairs,cntYEvenNumscntYOddNums乘以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)