Calculating Mr. Suantou's birthday

This code to calculate the day of the week is indeed relatively new to me. I hope that the big guys who can understand it can tell me how this works.

After typing the code, I seem to think that the year, month, and day should be processed directly in 7 days, which is a very wonderful process.

//蒜头君的生日
#include<iostream>
using namespace std;

int whatday(int y, int m, int d)
{
    
    
	int ans;
	
	for(int i=1; i<y; i++)
	{
    
    
		if(( i % 100 != 0 && i % 4 == 0 ) || i % 400 == 0 )
		{
    
    
			ans += 366 % 7;
			ans %= 7;
		}
		else
		{
    
    
			ans += 365 % 7;
			ans %= 7;
		}
	}
	
	for(int i=1; i<m; i++)
	{
    
    
		if( i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12 )
		{
    
    
			ans += 31 % 7;
			ans %= 7;
		}
		else if( i == 4 || i == 6 || i == 9 || i == 11 )
		{
    
    
			ans += 30 % 7;
			ans %= 7;
		}
		else if( ( y % 100 != 0 && y % 4 == 0 ) || i % 400 == 0 )
		{
    
    
			ans += 29 % 7;
			ans %= 7;
		}
		else
		{
    
    
			ans += 28 % 7;
			ans %= 7;
		}
	}
	
	ans += (d-1) % 7;
	ans %= 7;
	
	return ans;
}

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 blog.csdn.net/m0_46161051/article/details/114435279