Java中的Euler#19项目(关闭1)
你会得到以下信息,但你可能更愿意自己做一些研究 1900年1月1日是星期一。 三十天是九月, 四月、六月和十一月。 其余的都有三十一个, 仅二月一日, 哪一个有二十八个,无论晴雨。 闰年是29岁。 闰年发生在任何可以被4整除的年份,但除非它可以被400整除,否则不会发生在一个世纪。 在二十世纪(1901年1月1日至2000年12月31日),有多少个星期日是在一个月的第一天 我得到了172分,答案是171分。我不确定可能是什么问题,我已经尝试了所有的方法,我一直得到172。谢谢你的帮助Java中的Euler#19项目(关闭1),java,calendar,Java,Calendar,你会得到以下信息,但你可能更愿意自己做一些研究 1900年1月1日是星期一。 三十天是九月, 四月、六月和十一月。 其余的都有三十一个, 仅二月一日, 哪一个有二十八个,无论晴雨。 闰年是29岁。 闰年发生在任何可以被4整除的年份,但除非它可以被400整除,否则不会发生在一个世纪。 在二十世纪(1901年1月1日至2000年12月31日),有多少个星期日是在一个月的第一天 我得到了172分,答案是171分。我不确定可能是什么问题,我已经尝试了所有的方法,我一直得到172。谢谢你的帮助 publi
public static void main(String args[]){
int year=1901;
boolean isLeapYear=false;
int totalSundays=0;
int currentDay=1;//Starts on a Monday
while(year<=2000){
isLeapYear=false;
if((year%4)==0){
if((year%100)==0 && (year%400)==0){
isLeapYear=true;
} else if((year%100)==0 && (year%400)!=0){
isLeapYear=false;
} else {
isLeapYear=true;
}
}
System.out.println("The Year Is: "+year);
System.out.println("*******************************");
for(int month=1;month<=12;month++){
System.out.println("The Month is: "+month+" currentDay is : "+currentDay);
if(currentDay==7){
totalSundays++;
}
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
//January,March,May,July,August,October,December
currentDay+=3;
} else if(month==4 || month==6 || month==9 || month==11){
//April,June,September,November
currentDay+=2;
} else if(month==2 && isLeapYear){
//February has 29 days in a Leap Year
currentDay+=1;
}
if(currentDay>7){
currentDay=currentDay-7;
}
System.out.println("Updated Current Day Is : "+currentDay);
}
System.out.println("*******************************");
year++;
}
System.out.println("The total number of Sundays that fell in the first of the month is: "+totalSundays);
}
publicstaticvoidmain(字符串参数[]){
国际年=1901年;
布尔值isLeapYear=false;
int totalSundays=0;
int currentDay=1;//从星期一开始
虽然(年份你开始的日期不正确。1901年1月1日实际上是星期二(因此currentDay=2
),但进行更改会导致171:你开始的日期不正确。1901年1月1日实际上是星期二(因此currentDay=2
),如果不使用日历进行更改,则会导致171:
static int getDays(int month, int year){
switch (month){
case 4:case 6: case 9: case 11:
return 30;
case 2:
return (isLeapYear(year))?29:28;
}
return 31;
}
static boolean isLeapYear(int year){
boolean leap = false;
if(year%4==0){
if(year%100==0){
leap = (year%400==0)?true:false;
}
else{
leap = true;
}
}
return leap;
}
static int q19(){
int year = 1901;
// Sun:1 Mon: 2 ...
int firstDay = 3; // 1 Jan 1901 was Tuesday
int sundays = 0;
for(int i=year;i<2001;i++){
int days_In_Month = getDays(1,i);
int dif = days_In_Month%7;
if(i!=year)
firstDay = (dif+firstDay)%7;
if(firstDay == 1) sundays++;
for(int m = 2;m<=12;m++){
firstDay = (dif+firstDay)%7;
if(firstDay == 1) sundays++;
days_In_Month = getDays(m,i);
dif = days_In_Month%7;
}
}
return sundays;
}
static int getDays(int月,int年){
开关(月){
案例4:案例6:案例9:案例11:
返回30;
案例2:
返回时间(年)29:28;
}
返回31;
}
静态布尔值isLeapYear(整数年){
布尔跳跃=假;
如果(第%4年==0){
如果(年份%100==0){
闰年=(年份%400==0)?真:假;
}
否则{
闰=真;
}
}
返回跳跃;
}
静态int q19(){
国际年=1901年;
//星期一:1星期一:2。。。
int firstDay=3;//1901年1月1日是星期二
int sundays=0;
对于(int i=年;i不使用日历的情况下,它将是:
static int getDays(int month, int year){
switch (month){
case 4:case 6: case 9: case 11:
return 30;
case 2:
return (isLeapYear(year))?29:28;
}
return 31;
}
static boolean isLeapYear(int year){
boolean leap = false;
if(year%4==0){
if(year%100==0){
leap = (year%400==0)?true:false;
}
else{
leap = true;
}
}
return leap;
}
static int q19(){
int year = 1901;
// Sun:1 Mon: 2 ...
int firstDay = 3; // 1 Jan 1901 was Tuesday
int sundays = 0;
for(int i=year;i<2001;i++){
int days_In_Month = getDays(1,i);
int dif = days_In_Month%7;
if(i!=year)
firstDay = (dif+firstDay)%7;
if(firstDay == 1) sundays++;
for(int m = 2;m<=12;m++){
firstDay = (dif+firstDay)%7;
if(firstDay == 1) sundays++;
days_In_Month = getDays(m,i);
dif = days_In_Month%7;
}
}
return sundays;
}
static int getDays(int月,int年){
开关(月){
案例4:案例6:案例9:案例11:
返回30;
案例2:
返回时间(年)29:28;
}
返回31;
}
静态布尔值isLeapYear(整数年){
布尔跳跃=假;
如果(第%4年==0){
如果(年份%100==0){
闰年=(年份%400==0)?真:假;
}
否则{
闰=真;
}
}
返回跳跃;
}
静态int q19(){
国际年=1901年;
//星期一:1星期一:2。。。
int firstDay=3;//1901年1月1日是星期二
int sundays=0;
对于(int i=year;iThanks for the help,是的,它实际上从星期二开始,因为1900年1月1日是星期一,1900年有365天,所以1901年1月1日应该是星期二。非常有趣的是,许多人本能地认为1901年1月1日是星期一,而不是检查。谢谢你的帮助,是的,它实际上从星期二开始,given 1900年1月1日是星期一,1900年有365天,所以1901年1月1日应该是星期二。有趣的是,有多少人本能地认为1901年1月1日是星期一,而不是检查。仅供参考,如果您消除了神奇数字并使用final int sFYI,如果您消除了幻数并使用final int
s,您/我们将更容易遵循和维护此操作