- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在做欧拉计划#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。任何人都可以给我一些提示吗?还是我的代码有问题?
最佳答案
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/
我有基于选项卡栏的应用程序。当我放入 plist UIBackgroundModes 时(我的应用程序应该支持 voip),GUI 不会加载。它在启动屏幕上很糟糕,并且不会加载第一个选项卡 View
我是一名优秀的程序员,十分优秀!