计算一年末日的程序
世界末日可以指一个假设的事件,根据该事件,人类生命的终结是最有可能的。编写了许多算法来计算一年中一周中的哪一天最有可能在那天发生世界末日。
所有陈述均与公历有关。由于公历每 400 年重复一次,因此仅针对第 400 年确定了一套规则。算法源自约翰康威、刘易斯卡罗尔和历史上许多其他从事末日计算工作的数学家的计算。
要计算特定年份的末日,使用以下算法:-
- 提取年份的最后两位数字。(假设为 y)
- 除以 12 取值的下限。
- 然后将 y 除以 12 的余数相加。
- 计算 y 除以 12 的余数除以 4 的结果。
- 取上述值的地板,然后添加。
- 除以 7 (mod 7) 后取余数。
- 添加从星期日开始的一周中的锚日的值(将星期日视为0)
公式变为 -
这里 [ ] 是最大整数函数。
锚日在 100 年后变化,并在每 400 年后以下列方式重复 -
0-99 yrs --> Tuesday
100-199 yrs --> Sunday
200-299 yrs --> Friday
300-399 yr --> Wednesday
此后,如文章开头所述,上述锚定日重复。
例子:
Input : 2005
Output : Doomsday in the year 2005 = Monday
Input : 1800
Output : Doomsday in the year 1800 = Friday
下面是实现。
C++14
#include
using namespace std;
string dooms_day(int year)
{
// map to store days value of
// anchor day can be known
map dict_day;
dict_day[0] = "Sunday";
dict_day[1] = "Monday";
dict_day[2] = "Tuesday";
dict_day[3] = "Wednesday";
dict_day[4] = "Thursday";
dict_day[5] = "Friday";
dict_day[6] = "Saturday";
// Gregorian calendar repeats
// every 400 years
int k = year % 400;
int anchor;
// Decide the anchor day
if(k >= 0 && k < 100)
anchor = 2;
else if(k >= 100 && k < 200)
anchor = 0;
else if(k >= 200 && k < 300)
anchor = 5;
else
anchor = 3;
int y = year % 100;
// Dooms day formula by Conway
int doomsday = ((y / 12 + y % 12 +
(y % 12) / 4) % 7 + anchor) % 7;
return dict_day[doomsday];
}
// Driver code
int main()
{
int year = 1966;
cout << "Doomsday in the year "
<< year << " = " << dooms_day(year);
return 0;
}
// This code is contributed by yatinagg
Java
import java.util.*;
class GFG{
public static String dooms_day(int year)
{
// map to store days value of
// anchor day can be known
HashMap dict_day = new HashMap<>();
dict_day.put(0, "Sunday");
dict_day.put(1, "Monday");
dict_day.put(2, "Tuesday");
dict_day.put(3, "Wednesday");
dict_day.put(4, "Thursday");
dict_day.put(5, "Friday");
dict_day.put(6, "Saturday");
// Gregorian calendar repeats
// every 400 years
int k = year % 400;
int anchor;
// Decide the anchor day
if (k >= 0 && k < 100)
anchor = 2;
else if (k >= 100 && k < 200)
anchor = 0;
else if (k >= 200 && k < 300)
anchor = 5;
else
anchor = 3;
int y = year % 100;
// Dooms day formula by Conway
int doomsday = ((y / 12 + y % 12 +
(y % 12) / 4) % 7 +
anchor) % 7;
return dict_day.get(doomsday);
}
// Driver code
public static void main(String[] args)
{
int year = 1966;
System.out.println("Doomsday in the year " +
year + " = " +
dooms_day(year));
}
}
// This code is contributed divyeshrabadiya07
Python3
def dooms_day(year):
# dictionary to store days
# value of anchor day can be known
dict_day ={ 0 : "Sunday",
1 : "Monday",
2 : "Tuesday",
3 : "Wednesday",
4 : "Thursday",
5 : "Friday",
6 : "Saturday" }
# gregorian calendar repeats
# every 400 years
k = year % 400
# decide the anchor day
if(k >= 0 and k < 100):
anchor = 2
elif(k >= 100 and k < 200):
anchor = 0
elif(k >= 200 and k < 300):
anchor = 5
else:
anchor = 3
y = year % 100
# dooms day formula by Conway
doomsday = ((y//12 + y % 12 + (y % 12)//4)% 7 + anchor) % 7
return dict_day[doomsday]
# Driver code
year = 1966
print("Doomsday in the year % s = % s"%(year,
dooms_day(year)))
C#
using System;
using System.Collections.Generic;
class GFG
{
static String dooms_day(int year)
{
// map to store days value of
// anchor day can be known
Dictionary dict_day =
new Dictionary();
dict_day.Add(0, "Sunday");
dict_day.Add(1, "Monday");
dict_day.Add(2, "Tuesday");
dict_day.Add(3, "Wednesday");
dict_day.Add(4, "Thursday");
dict_day.Add(5, "Friday");
dict_day.Add(6, "Saturday");
// Gregorian calendar repeats
// every 400 years
int k = year % 400;
int anchor;
// Decide the anchor day
if (k >= 0 && k < 100)
anchor = 2;
else if (k >= 100 && k < 200)
anchor = 0;
else if (k >= 200 && k < 300)
anchor = 5;
else
anchor = 3;
int y = year % 100;
// Dooms day formula by Conway
int doomsday = ((y / 12 + y % 12 +
(y % 12) / 4) % 7 +
anchor) % 7;
return dict_day[doomsday];
}
// Driver code
static void Main()
{
int year = 1966;
Console.WriteLine("Doomsday in the year " +
year + " = " +
dooms_day(year));
}
}
// This code is contributed by divyesh072019
Javascript
输出:
Doomsday in the year 1966 = Monday