(壹)判断某天是星期几
基本思路:
首先,我们先选择一个基准1900年1月1日(周一)
其次,我们需要算出X年X月X日与1900年1月1日相差多少天(D1),(D1)包括:(1)X年1月1日与1900年1月1日相差多少天(D2)(2)X年X月1日与X年1月1日相差多少天(D2)(3)X年X月X日与X年X月1日相差多少天(D3)。计算D1需要判断平闰年并用循环相加,计算D2需要构造一个关于平闰年月份天数的数组并用循环相加,D3=X日中的X。
最后,我们取D1除以7的余数,余数是几就是星期几(余0即周日)。
代码如下:
#include<stdio.h>
//判断平闰年
int leap_(int year)
{
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return 1;
else
return 0;
}
//算出D1
int D1_(int year)
{
int i = 0, D1 = 0;
for(i = 1900; i < year; i++)
{
if(leap_(i) == 1)
D1 += 366;
else
D1 += 365;
}
return D1;
}
//算出D2
int D2_(int year, int month)
{
int l_[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//l代表leap闰年
int c_[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//c代表common平年
int i = 0,D2 = 0;
int leap = leap_(year);
for(i = 1; i < month; i++)
{
if(leap == 1)
D2 += l_[i];
else
D2 += c_[i];
}
return D2;
}
//定义一个输出除7余几的函数
int re_(int D)
{
return D % 7;
}
int main()
{
int year, month, day;
scanf("%d %d %d", &year, &month, &day);
int D = 0, re = 0;