1308HDU
程序员文章站
2022-06-23 14:38:46
1 判断闰年的方法,分为两种,1582年及之前是旧的规则,1582年之后是新的规则2 总的思路:根据题目中给的输出数据知道1752.9.2是星期三,1752.9.14是星期四,所以将将给的日期划分为这两段,在1752.9.2号前的日期是一种计算方法,在1752.9.14之后的是一种计算方法3计算给的日期与1752.9.2号之间有多少天或者与1752.9.14号之间有多少天,然后根据天数来确定是星期几关于天数的计算:日期在1752.9.2及之前的a.年份小于1752的b.年份等于1752月份小于....
- 1 判断闰年的方法,分为两种,1582年及之前是旧的规则,1582年之后是新的规则
- 2 总的思路:根据题目中给的输出数据知道1752.9.2是星期三,1752.9.14是星期四,所以将将给的日期划分为这两段,在1752.9.2号前的日期是一种计算方法,在1752.9.14之后的是一种计算方法
- 3计算给的日期与1752.9.2号之间有多少天或者与1752.9.14号之间有多少天,然后根据天数来确定是星期几
关于天数的计算:
日期在1752.9.2及之前的
a.年份小于1752的
b.年份等于1752月份小于9的
c.年份等于1752月份等于9的
日期在1752.9.14及之后的
a.年份大于1752的
b.年份等于1752月份大于9的
c.年份等于1752月份等于9的 - 4检查错误日期:年<=0, 错误
月<=0或者>12,错误
日则根据月份和年份来确定,在闰年注意2月
1752年9月份2号和14号之间是没有的
#include<iostream>
#include<string>
using namespace std;
string week1[7]= {"Wednesday","Tuesday","Monday","Sunday","Saturday","Friday","Thursday"};
string week2[7]= {"Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednesday"};
int monthDay1[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
int monthDay2[13]= {0,31,29,31,30,31,30,31,31,30,31,30,31};
string mon[13]= {"","January","February","March","April","May","June","July","August","September","October","November","December"};
bool isrunnian(int year) {
if(year<=1582)return year%4==0;
else return ((year%100!=0&&year%4==0)||(year%400==0));
}
bool check(int month,int day,int year) {
if(year<=0||month<=0||day<=0||month>12)return false;
if(isrunnian(year)&&day>monthDay2[month])return false;
if(!isrunnian(year)&&day>monthDay1[month])return false;
if(year==1752&&month==9&&(day>2&&day<14))return false;
return true;
}
void weekjudge(int month,int day,int year) {
int sum=0,i;
if(year<1752||(year==1752&&month<9)||(year==1752&&month==9&&day<=2)) {
if(year<1752) {
sum+=2;
for(int i=8; i>=1; i--)
sum+=monthDay2[i];
for(int i=1751; i>=year+1; i--)
sum+=(isrunnian(i)?366:365);
sum+=(isrunnian(year)?monthDay2[month]-day:monthDay1[month]-day);
for(int i=month+1; i<=12; i++)
sum+=(isrunnian(year)?monthDay2[i]:monthDay1[i]);
} else if(year==1752&&month<9) {
sum+=(isrunnian(year)?monthDay2[month]-day:monthDay1[month]-day);
for(int i=month+1; i<=8; i++)
sum+=(isrunnian(year)?monthDay2[i]:monthDay1[i]);
sum+=2;
} else {
sum+=(2-day);
}
cout<<mon[month]<<" "<<day<<", "<<year<<" is a "<<week1[sum%7]<<endl;
} else {
if(year>1752) {
sum+=16;
for(int i=10; i<=12; i++)
sum+=monthDay2[i];
for(int i=1753; i<=year-1; i++)
sum+=(isrunnian(i)?366:365);
sum+=day;
for(int i=1; i<=month-1; i++)
sum+=(isrunnian(year)?monthDay2[i]:monthDay1[i]);
} else if(year==1752&&month>9) {
sum+=16;
for(int i=10; i<=month-1; i++)
sum+=monthDay2[i];
sum+=day;
} else {
sum+=(day-14);
}
cout<<mon[month]<<" "<<day<<", "<<year<<" is a "<<week2[sum%7]<<endl;
}
}
int main() {
int month,year,day;
while(cin>>month>>day>>year&&(month||day||year)) {
if(check(month,day,year)==false) {
cout<<month<<"/"<<day<<"/"<<year<<" is an invalid date."<<endl;
} else {
weekjudge(month,day,year);
}
}
return 0;
}
本文地址:https://blog.csdn.net/m0_50498666/article/details/108993309