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

📅  最后修改于: 2021-04-26 18:56:54             🧑  作者: 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)