给定表示年份的整数Y作为输入,任务是查找该年的复活节日期。
例子:
Input: Y = 2020
Output: 2020-04-12
Explanation:
In 2020, Easter was on 12 April
Hence, Easter date will be 2020-04-12
Input: Y = 1991
Output: 1991-03-30
Explanation:
In 1991, Easter was on 30 March
Hence, Easter date will be 1991-03-30
方法:高斯复活节算法用于轻松计算一年的复活节日期。该算法最早是由卡尔·弗里德里希·高斯(Carl Friedrich Gauss)想到的。对于高斯如何着陆该算法,没有适当的解释,但是事实证明,该算法的实现非常准确。
高斯复活算法的详细说明如下:
- 首先,计算Y在年份中的位置。
A = Y mod 19
- 现在,根据儒略历找到find日数。
B = Y mod 4
- 然后,让我们考虑到非-年比52周长一天。
C = Y mod 7
- M取决于Y年的世纪。对于19世纪,M =23。对于21世纪,M = 24,依此类推。
它是使用以下关系式计算的:
P = floor (Y / 100)
Q = ((13 + 8 * P) / 25)
M = (15 – Q + P – (P / 4)) mod 30
- 儒略历和公历之间calendar日的差额为:
N = (4 + P – (P / 4)) mod 7
- 要查找3月21日的“复活节”满月日期,需要加上的天数为:
D = (19*A + M) mod 30
- 而且,从Paschal满月到下一个星期日的天数为:
E = (N + 2*B + 4*C + 6*D) mod 7
- 因此,使用D ans E ,复活节星期日的日期将是三月(22 + D + E) 。如果这个数字大于31,那么我们移到四月。
- 现在农历正好不是30天,而是少于30天。因此,要消除这种不一致,请遵循以下几种情况:
if (D == 29) and (E == 6)
return “April 19”
else if (D == 28) and (E == 6)
return “April 18”
下面是上述方法的实现:
C++
// C++ program for the
// above approach
#include
#include
using namespace std;
// Function calculates and prints
// easter date for given year Y
void gaussEaster(int Y)
{
float A, B, C, P, Q,
M, N, D, E;
// All calculations done
// on the basis of
// Gauss Easter Algorithm
A = Y % 19;
B = Y % 4;
C = Y % 7;
P = (float)floor(Y / 100);
Q = (float)floor((13 + 8 * P) / 25);
M = (int)(15 - Q + P - P / 4) % 30;
N = (int)(4 + P - P / 4) % 7;
D = (int)(19 * A + M) % 30;
E = (int)(2 * B + 4 * C + 6 * D + N) % 7;
int days = (int)(22 + D + E);
// A corner case,
// when D is 29
if ((D == 29) && (E == 6)) {
cout << Y << "-04-19";
return;
}
// Another corner case,
// when D is 28
else if ((D == 28) && (E == 6)) {
cout << Y << "-04-18";
return;
}
else {
// If days > 31, move to April
// April = 4th Month
if (days > 31) {
cout << Y << "-04-"
<< (days - 31);
return;
}
else {
// Otherwise, stay on March
// March = 3rd Month
cout << Y << "-03-"
<< days;
return;
}
}
}
// Driver Code
int main()
{
int Y = 2020;
gaussEaster(Y);
return 0;
}
Java
// Java program for the
// above approach
import java.util.Date;
import java.util.Scanner;
// Function calculates and prints
// easter date for given year Y
public class GaussEaster {
static void gaussEaster(int Y)
{
float A, B, C, P, Q,
M, N, D, E;
// All calculations done
// on the basis of
// Gauss Easter Algorithm
A = Y % 19;
B = Y % 4;
C = Y % 7;
P = (float)Math.floor(Y / 100);
Q = (float)Math.floor(
(13 + 8 * P) / 25);
M = (15 - Q + P - P / 4) % 30;
N = (4 + P - P / 4) % 7;
D = (19 * A + M) % 30;
E = (2 * B + 4 * C + 6 * D + N) % 7;
int days = (int)(22 + D + E);
// A corner case,
// when D is 29
if ((D == 29) && (E == 6)) {
System.out.println(Y + "-04"
+ "-19");
return;
}
// Another corner case,
// when D is 28
else if ((D == 28) && (E == 6)) {
System.out.println(Y + "-04"
+ "-18");
return;
}
else {
// If days > 31, move to April
// April = 4th Month
if (days > 31) {
System.out.println(Y + "-04-"
+ (days - 31));
return;
}
// Otherwise, stay on March
// March = 3rd Month
else {
System.out.println(Y + "-03-"
+ days);
return;
}
}
}
// Driver code
public static void main(String[] args)
{
int Y = 2020;
gaussEaster(Y);
}
}
Python3
# Python3 program for the
# above approach
import math
# Function calculates and prints
# easter date for given year Y
def gaussEaster(Y):
# All calculations done
# on the basis of
# Gauss Easter Algorithm
A = Y % 19
B = Y % 4
C = Y % 7
P = math.floor(Y / 100)
Q = math.floor((13 + 8 * P) / 25)
M = (15 - Q + P - P // 4) % 30
N = (4 + P - P // 4) % 7
D = (19 * A + M) % 30
E = (2 * B + 4 * C + 6 * D + N) % 7
days = (22 + D + E)
# A corner case,
# when D is 29
if ((D == 29) and (E == 6)):
print(Y, "-04-19")
return
# Another corner case,
# when D is 28
elif ((D == 28) and (E == 6)):
print(Y, "-04-18")
return
else:
# If days > 31, move to April
# April = 4th Month
if (days > 31):
print(Y, "-04-", (days - 31))
return
else:
# Otherwise, stay on March
# March = 3rd Month
print(Y, "-03-", days)
return
# Driver Code
Y = 2020
gaussEaster(Y)
# This code is contributed by code_hunt
C#
// C# program for the
// above approach
using System;
class GFG{
// Function calculates and prints
// easter date for given year Y
static void gaussEaster(int Y)
{
float A, B, C, P, Q,
M, N, D, E;
// All calculations done
// on the basis of
// Gauss Easter Algorithm
A = Y % 19;
B = Y % 4;
C = Y % 7;
P = (float)(Y / 100);
Q = (float)(
(13 + 8 * P) / 25);
M = (15 - Q + P - P / 4) % 30;
N = (4 + P - P / 4) % 7;
D = (19 * A + M) % 30;
E = (2 * B + 4 * C + 6 * D + N) % 7;
int days = (int)(22 + D + E);
// A corner case,
// when D is 29
if ((D == 29) && (E == 6))
{
Console.Write(Y + "-04" + "-19");
return;
}
// Another corner case,
// when D is 28
else if ((D == 28) && (E == 6))
{
Console.Write(Y + "-04" + "-18");
return;
}
else
{
// If days > 31, move to April
// April = 4th Month
if (days > 31)
{
Console.Write(Y + "-04-" +
(days - 31));
return;
}
// Otherwise, stay on March
// March = 3rd Month
else
{
Console.Write(Y + "-03-" + days);
return;
}
}
}
// Driver code
public static void Main(string[] args)
{
int Y = 2020;
gaussEaster(Y);
}
}
// This code is contributed by Ritik Bansal
2020-04-12
时间复杂度: O(1)
辅助空间: O(1)