# 用C实现简单的万年日历

2010年1月1日是星期五，为了简便，我们假设每一年都为365天，请编程实现，任意输入某年某月，打印出该月的第三个星期五是多少号？

#include <stdio.h>

int getEveryStartDay(int year)//获取每一年的第一天星期几
{
return (((year - 2010)*365 % 7 + 5) % 7);
}
int getEveryMonthDay(int year,int month)//获取某一年的某个月是星期几
{
int monthDay[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int sum = 0;
for(int i = 0;i < month - 1;i ++)
{
sum += monthDay[i];
}
return ((sum % 7 + getEveryStartDay(year)) % 7);
}
int main()
{
int y,m;

scanf("%d%d",&y,&m);

if(y < 2010 || m <= 0 || m > 12)
{
printf("the data you input is error.\n");
}

int startDay =  getEveryMonthDay(y,m);

if(startDay <= 5)
{
printf("%2d\n",14 + 5 - startDay);
}
else
{
printf("%2d\n",14 + 13 - startDay);
}

}



/*
* input: year
*
* if year is a leap year,return ture
*
* else return false
*/
bool IsOrNotLeapYear(int year)
{
if(year <= 0)
return ;
bool tag;
if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
{
tag = true;
}
else
{
tag = false;
}
}
else
{
tag = true;
}
}
else
{
tag = false;
}
return tag;
}

void IsOrNotPrimeNum(int nNum,int *dest,int *len)//int dest[100],len;IsOrNotPrimeNum(100,dest,&len);
{
if( (nNum < 2) || (dest == NULL) || (len == NULL) )
return ;
bool tag;
int nCount = 0;
for(int i = 2;i < nNum;i ++)
{
tag = false ;
for(int j = 2;j <= sqrt(i);j ++)
{
if(i % j == 0)
{
tag = true;
break;
}
}
if(tag == false)
{
dest[nCount ++] = i;
}
}
*len = nCount;
}

• 写评论