gpt4 book ai didi

java - 是否有计算任何一天是哪一天的公式?我被欧拉计划 #19 困住了

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:19:13 25 4
gpt4 key购买 nike

我正在做欧拉计划#19。这是状态:

您已获得以下信息,但您可能更愿意自己做一些研究。

1900 年 1 月 1 日是星期一。三十天是九月,四月、六月和十一月。其余的都有三十一个,单独拯救二月,其中有二十八,风雨无阻。在闰年,二十九。闰年出现在任何能被 4 整除的年份,但不会出现在世纪,除非它能被 400 整除。二十世纪(1901 年 1 月 1 日至 2000 年 12 月 31 日)的每个月的第一天有多少个星期日?

我在 Internet 上搜索了一些计算星期几的公式。我找到了 Zeller formula很出色。 W = [C/4] - 2C + y + [y/4] + [13 * (M+1)/5] + d - 1.(C 是世纪 + 1,y 是年份的最后两个数字)但是查1900.01.01不对,应该是星期一,但是根据公式,是6(也就是星期六)

我查了很多日期,几乎都适合这个公式。但是还有更少的日子不匹配。对于这种情况,我的 Java 代码如下:

package number;

public class CountingSundays {
public static int calculateWeek(int year, int month, int date){
int c = year/100;
int y = year%100;
int w = c/4-2*c+y+y/4+13*(month+1)/5+date-1;
w = (w%7 + 7)%7;
return w;
}
public static void main(String[] args) {
// int w = calculateWeek(1900, 01, 01);
// System.out.println(w);
int count = 0;
for(int i = 1901; i <= 2000; i++)
for(int j = 1; j <= 12; j++)
if(calculateWeek(i, j, 01) == 0)
count++;
System.out.println(count);
}
}

对于不匹配,我的输出是173,不是要求的结果171。任何人都可以给我一些提示吗?还是我的代码有问题?

最佳答案

wikipedia article you cited

m is the month (3 = March, 4 = April, 5 = May, ..., 14 = February)

For January 1, 2000, the date would be treated as the 13th month of 1999,
so the values would be:
q = 1
m = 13
K = 99
J = 19

所以如果你想使用这个公式,你可能应该相应地调整你的输入值,即添加这样的东西

if (month <= 2) {
month += 12;
year--;
}

关于java - 是否有计算任何一天是哪一天的公式?我被欧拉计划 #19 困住了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16376684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com