📜  给定年份范围内的Le年计数

📅  最后修改于: 2021-04-22 01:40:15             🧑  作者: Mango

给定为LR的两年,任务是找出(L,R)范围内(包括L和R)的of年总数。

例子:

天真的方法:这个想法是通过L到R进行迭代,并使用该方法检查年份是否为a年。
时间复杂度: O(N)

高效方法:

  1. 如果满足以下条件,则一年是a年:
    • 年是400的倍数。
    • Year是4的倍数,而不是100的倍数。
  2. 因此计算范围(1,L)和(1,R)中满足上述条件的数字为
  3. 范围(1,R)的Le年数与范围(1,L)的leap年数之差将是所需的输出。

下面是上述方法的实现。

C++
// C++ implementation to find the 
// count of leap years in given
// range of the year 
  
#include 
  
using namespace std;
  
// Function to calculate the number
// of leap years in range of (1, year)
int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    cout << num1 - num2 << endl;
}
  
// Driver Code
int main()
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
  
    return 0;
}


Java
// Java implementation to find the 
// count of leap years in given
// range of the year 
class GFG
{
  
// Function to calculate the number
// of leap years in range of (1, year)
static int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
static void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    System.out.print(num1 - num2 +"\n");
}
  
// Driver Code
public static void main(String[] args)
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
}
}
  
// This code is contributed by PrinciRaj1992


Python3
# Python3 implementation to find the 
# count of leap years in given 
# range of the year 
  
# Function to calculate the number 
# of leap years in range of (1, year) 
def calNum(year) : 
  
    return (year // 4) - (year // 100) + (year // 400); 
  
# Function to calculate the number 
# of leap years in given range 
def leapNum(l, r) :
  
    l -= 1; 
    num1 = calNum(r); 
    num2 = calNum(l); 
    print(num1 - num2); 
  
# Driver Code 
if __name__ == "__main__" : 
  
    l1 = 1; r1 = 400; 
    leapNum(l1, r1); 
  
    l2 = 400; r2 = 2000; 
    leapNum(l2, r2); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation to find the 
// count of leap years in given
// range of the year 
using System;
  
class GFG
{
  
// Function to calculate the number
// of leap years in range of (1, year)
static int calNum(int year)
{
    return (year / 4) - (year / 100) +
                        (year / 400);
}
  
// Function to calculate the number
// of leap years in given range
static void leapNum(int l, int r)
{
    l--;
    int num1 = calNum(r);
    int num2 = calNum(l);
    Console.Write(num1 - num2 +"\n");
}
  
// Driver Code
public static void Main(String[] args)
{
    int l1 = 1, r1 = 400;
    leapNum(l1, r1);
  
    int l2 = 400, r2 = 2000;
    leapNum(l2, r2);
}
}
  
// This code is contributed by PrinciRaj1992


输出:
97
389

时间复杂度: O(1)