您将获得以下信息,但您可能更愿意为自己做一些研究.
1900年1月1日是星期一.三十天有九月,四月,六月和十一月.所有其他人都有三十一人,仅拯救二月,其中二十八,风雨无阻.在闰年,二十九岁.闰年可以被4整除,但除非可以被400整除,否则不是一个世纪.在二十世纪的第一个月(1901年1月1日至2000年12月31日),有多少个星期日下降?
我已经得到172,答案是171.我不确定可能是什么问题,我已经尝试了一切,我一直得到172.感谢您的帮助.
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2169 次 |
| 最近记录: |