📜  从第一年为星期一的基准年中找到给定年份的第一天

📅  最后修改于: 2021-04-29 12:52:59             🧑  作者: Mango

鉴于两个整数YB代表两年,任务是找到一周的日子,一年y位于假设一年B的1月1是周一的01月01

例子:

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

  1. 位于基准年(B)年份(Y)之间的总年等于(Y – 1)–B
  2. 介于之间的of年总数=年总数/ 4
  3. 介于两者之间的非-年总数=总年– ap年
  4. 总天数=总Le年* 366 +非-年* 365 + 1
  5. 因此, Y的1月1这一天是合计天数%7

下面是上述方法的实现:

C++14
// C++ Implementation of
// the above approach
 
#include 
using namespace std;
 
// Function to find the day of
// 1st January of Y year
void findDay(int Y, int B)
{
    int lyear, rest, totaldays, day;
 
    // Count years between
    // years Y and B
    Y = (Y - 1) - B;
 
    // Count leap years
    lyear = Y / 4;
 
    // Non leap years
    rest = Y - lyear;
 
    // Total number of days in the years
    // lying between the years Y and B
    totaldays = (rest * 365)
                + (lyear * 366) + 1;
 
    // Actual day
    day = (totaldays % 7);
 
    if (day == 0)
        printf("Monday");
 
    else if (day == 1)
        printf("Tuesday");
 
    else if (day == 2)
        printf("Wednesday");
 
    else if (day == 3)
        printf("Thursday");
 
    else if (day == 4)
        printf("Friday");
 
    else if (day == 5)
        printf("Saturday");
 
    else if (day == 6)
        printf("Sunday");
 
    else
        printf("INPUT YEAR IS WRONG!");
}
 
// Driver Code
int main()
{
    int Y = 2020, B = 1900;
    findDay(Y, B);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
 
  // Function to find the day of
  // 1st January of Y year
  static void findDay(int Y, int B)
  {
    int lyear, rest, totaldays, day;
 
    // Count years between
    // years Y and B
    Y = (Y - 1) - B;
 
    // Count leap years
    lyear = Y / 4;
 
    // Non leap years
    rest = Y - lyear;
 
    // Total number of days in the years
    // lying between the years Y and B
    totaldays = (rest * 365)
      + (lyear * 366) + 1;
 
    // Actual day
    day = (totaldays % 7);
 
    if (day == 0)
      System.out.println("Monday");
 
    else if (day == 1)
      System.out.println("Tuesday");
 
    else if (day == 2)
      System.out.println("Wednesday");
 
    else if (day == 3)
      System.out.println("Thursday");
 
    else if (day == 4)
      System.out.println("Friday");
 
    else if (day == 5)
      System.out.println("Saturday");
 
    else if (day == 6)
      System.out.println("Sunday");
 
    else
      System.out.println("INPUT YEAR IS WRONG!");
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int Y = 2020, B = 1900;
    findDay(Y, B);
  }
}
 
// This code is contributed by code_hunt.


Python3
# Python program to implement
# the above approach
 
# Function to find the day of
# 1st January of Y year
def findDay(Y, B):
    lyear, rest, totaldays, day = 0, 0, 0, 0;
 
    # Count years between
    # years Y and B
    Y = (Y - 1) - B;
 
    # Count leap years
    lyear = Y // 4;
 
    # Non leap years
    rest = Y - lyear;
 
    # Total number of days in the years
    # lying between the years Y and B
    totaldays = (rest * 365) + (lyear * 366) + 1;
 
    # Actual day
    day = (totaldays % 7);
 
    if (day == 0):
        print("Monday");
 
    elif (day == 1):
        print("Tuesday");
 
    elif (day == 2):
        print("Wednesday");
 
    elif (day == 3):
        print("Thursday");
 
    elif (day == 4):
        print("Friday");
 
    elif (day == 5):
        print("Saturday");
 
    elif (day == 6):
        print("Sunday");
 
    else:
        print("INPUT YEAR IS WRONG!");
 
# Driver code
if __name__ == '__main__':
    Y = 2020; B = 1900;
    findDay(Y, B);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG
{
  // Function to find the day of
  // 1st January of Y year
  static void findDay(int Y, int B)
  {
    int lyear, rest, totaldays, day;
 
    // Count years between
    // years Y and B
    Y = (Y - 1) - B;
 
    // Count leap years
    lyear = Y / 4;
 
    // Non leap years
    rest = Y - lyear;
 
    // Total number of days in the years
    // lying between the years Y and B
    totaldays = (rest * 365)
      + (lyear * 366) + 1;
 
    // Actual day
    day = (totaldays % 7);
    if (day == 0)
      Console.WriteLine("Monday");
    else if (day == 1)
      Console.WriteLine("Tuesday");
    else if (day == 2)
      Console.WriteLine("Wednesday");
    else if (day == 3)
      Console.WriteLine("Thursday");
    else if (day == 4)
      Console.WriteLine("Friday");
    else if (day == 5)
      Console.WriteLine("Saturday");
    else if (day == 6)
      Console.WriteLine("Sunday");
    else
      Console.WriteLine("INPUT YEAR IS WRONG!");
  }
 
// Driver code
static void Main()
{
    int Y = 2020, B = 1900;
    findDay(Y, B);
}
}
 
// This code is contribute by susmitakundugoaldanga


输出:
Wednesday

时间复杂度: O(1)
辅助空间: O(1)