Do not hit the table, keep the laws of The New Solution: Zeller's congruence
as is known to all, Zeller is a formula for solving any day of the week a few powerful formula of (including the special case of leap year)
Formula description:
week=y+[y/4]+[c/4]-2c+[13*(m+1)/5]+day-1
week: Week, Week 7 to obtain modulo: 0 Sunday, Monday, 1-, 2- Tuesday, etc;
c (abbreviation of the English word century the century): year% 100 applies only to future 16c
year: year% 100 years (the last two digits)
E.g., 2019 c is 20, y is 19
month: month (month greater than or equal to 3, 14 or less, that Zeller's formula, a 1-year, on February 13 to be seen as a year, 14 months to calculate, such as January 1, 2003 to look for 2002 of 13 May 1 is calculated)
day: Day [] represents rounding, i.e., as long as the integer part.
Look at an example, solving October 1, 2049 is the day of the week:
week=y+[y/4]+[c/4]-2c+[13(month+1)/5]+day-1
week=49+[49/4]+[20/4]-2×20+[13×(10+1)/5]+1-1
week=49+[12.25]+5-40+[28.6]
week=49+12+5-40+28
week = 54% 7 I 5
It is Friday
Code:
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
int year,month,sunday;
int main(){
//freopen("mother.txt","r",stdin);
scanf("%d",&year);
int c=year/100;
int y=year%100;
month=5;//直接把月份锁定在5月
rep(day,1,31){//枚举5月的所有日期(5月大,31天)
int week=(c/4)-2*c+(y+y/4)+(13*(month+1))/5+day-1;//蔡勒公式
while(week<0)week+=7;//避免week为负
week%=7;
if(week==0)sunday++;//week==0即星期天,week不是==7,因为7%7==0
if(sunday==2){//5月的第二个星期天,已找到
printf("%d",day);
break;
}
}
return 0;
}