#include <cstdio>
int months[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
int isrun(int year){
if (year %400==0 || (year%4==0 && year%100!=0))return 1;
else return 0;
}
void addoneday(int &year,int &month,int &day){
day++;
if (day>months[isrun(year)][month]){
month++;
day = 1;
}
if (month>12){
month=1;
year++;
}
}
void suboneday(int &year,int &month,int &day){
day--;
if (day==0){
month--;
//这里不应该直接对day进行赋值,应该月份确定下来再赋值,因为月份有可能也不够减
//day = months[isrun(year)][month];
}
if (month==0){
year--;
month = 12;
}
if (day==0)day = months[isrun(year)][month];
}
// 计算所在年份的第几天
int cal_(int& year ,int &month,int &day){
int cnt=0;
while (month!=1||day!=1){
suboneday(year,month,day);
cnt++;
}
cnt++;
return cnt;
}
int isbefore(int year_1,int year_2,int month_1,int month_2,int day_1,int day_2){
if (year_1!=year_2) return year_1<year_2;
if (month_1!=month_2)return month_1<month_2;
return day_1<day_2;
}
int which_week(int year,int month,int day){
// 可以从题目中的输入输出得到一天为周几
int y = 2021;
int m = 5;
int d = 1;
int dayofweek = 6;
if (isbefore(year,y,month,m,day,d)){
while (year!=y || month!= m||day !=d){
suboneday(y,m,d);
dayofweek--;
if (dayofweek==-1)dayofweek = 6;
}
}else{
while (year!=y || month!= m||day !=d){
addoneday(y,m,d);
dayofweek++;
if (dayofweek==7)dayofweek = 0;
}
}
return dayofweek;
}
int main(){
/*
int year,month,day;
scanf("%d-%d-%d",&year,&month,&day);
int n;
scanf("%d",&n);
for (int i=0;i<n;i++)suboneday(year,month,day);
printf("%4d-%02d-%02d",year,month,day);
*/
int year,month,day;
scanf("%d-%d-%d",&year,&month,&day);
printf("%d",which_week(year,month,day));
return 0;
}
常见日期处理
最新推荐文章于 2024-11-04 23:24:03 发布