Chua Kim Larsen formula

With one subject, to understand this formula:

  There is a formula can quickly calculate the date one day of the week it is, this is referred to Cai Kim Larsen formula , assuming a weekly w, the year of y, for the month m, date d.
              w = (d + 2 × m
+ 3 × (m + 1) / 5 + y + y / 4-y / 100 + y / 400)% 7   then the calculated w is the real plus 1 week of . Note that each year 1 to February 13 as the previous year, calculated 14 months, above the division are divisible.

 

#include <iostream>
#include <string>
using namespace std;
int whatday(int y, int m, int d) {
    if(m == 1) y--,m = 13;
    else if(m == 2) y--,m = 14;
    return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
}
string weekday[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int main() {
    int y, m, d;
    cin >> y >> m >> d;
    cout << weekday[whatday(y, m, d)] << endl;
    return 0;
}

  

 

Guess you like

Origin www.cnblogs.com/apex-wzw/p/11488235.html
Kim