唯有前进值得敬仰

---等得越久,相聚时越幸福
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

已知某天是星期几,求任意一天是星期几

Posted on 2010-12-20 16:48  绿豆芽33  阅读(1859)  评论(0编辑  收藏  举报

问题如题,比如已知今天是周一,那么判断历史上或未来是的任一天是周几?
这个问题不难,但是写起代码来还是有点复杂的,好多人在笔试的时候都是放弃该题的。

趁自己有空的时候写了一下,贴出来共享。程序写得比较简单,没有进行什么错误检查。

执行例子:

enter a date:year month day=======2010 12 20回车

week ?1回车
enter another date but don't know on which weekday======2009 12 1回车

代码
/*********************************************************
*给定一天是周几,判断历史上或未来某天是周几
*date:2010.12.8
*author:wyh
*********************************************************
*/

#include
<iostream>
using namespace std;

struct Date{
int year,month,day;
};

enum Week{
MON
=1,
TUE,
WED,
THU,
FRI,
SAT,
SUN,
};

int isLeapYear(int y)
{
if((y%4==0&&y%100!=0)||y%400==0)return 1;
else return 0;
}

int DaysGone(Date d,int *restdays)
{
/*
*返回值是某个日期在一年中是第多少天
*restdays是这年还剩多少天
*/
int leap=0;
int days;
if(isLeapYear(d.year))leap=1;
switch(d.month){
case 1:days=d.day;break;
case 2:days=d.day+31;break;
case 3:if(leap)days=d.day+29+31;
else days=d.day+28+31;
break;
case 4:if(leap)days=d.day+31+29+31;
else days=d.day+31+28+31;
break;
case 5:if(leap)days=d.day+30+31+29+31;
else days=d.day+30+31+28+31;
break;
case 6:if(leap)days=d.day+31+30+31+29+31;
else days=d.day+31+30+31+28+31;
break;
case 7:if(leap)days=d.day+30+31+30+31+29+31;
else days=d.day+30+31+30+31+28+31;
break;
case 8:if(leap)days=d.day+31+30+31+30+31+29+31;
else days=d.day+31+30+31+30+31+28+31;
break;
case 9:if(leap)days=d.day+31+31+30+31+30+31+29+31;
else days=d.day+31+31+30+31+30+31+28+31;
break;
case 10:if(leap)days=d.day+30+31+31+30+31+30+31+29+31;
else days=d.day+30+31+31+30+31+30+31+28+31;
break;
case 11:if(leap)days=d.day+31+30+31+31+30+31+30+31+29+31;
else days=d.day+31+30+31+31+30+31+30+31+28+31;
break;
case 12:if(leap)days=d.day+30+31+30+31+31+30+31+30+31+29+31;
else days=d.day+30+31+30+31+31+30+31+30+31+28+31;
break;
}
if(leap)*restdays=366-days;
else *restdays=365-days;
return days;
}

int ComputeDays(Date early,Date later)
{
int leapcount=0;
int y,earlyrestdays=0,laterrestdays=0,latergonedays=0;
int result;
for(y=early.year+1;y<later.year;y++)
if(isLeapYear(y))leapcount++;//不包含要求计算的两年
DaysGone(early,&earlyrestdays);
latergonedays
=DaysGone(later,&laterrestdays);
result
=latergonedays+earlyrestdays;
result
+=(later.year-early.year-1)*365+leapcount;
return result;
}

int WhoEarly(Date d1,Date d2)
{
//d1早则返回1,d2则0,=则-1
int res=0;
int y,m,d;
y
=d1.year-d2.year;
m
=d1.month-d2.month;
d
=d1.day-d2.day;
if(y<0)res=1;
else if(y==0){
if(m<0)res=1;
else if(m==0){
if(d<0)res=1;
else if(d==0)res=-1;
else res=0;
}
//else if
else res=0;
}
//else if
else res=0;
}

int Mod7(Date d1,Date d2,int weekday)
{
//此处d1是周几为已知数weekday
int tmp,res;
tmp
=WhoEarly(d1,d2);
if(tmp==-1)res=0;
else if(tmp==1)res=ComputeDays(d1,d2)%7;
else res=7-ComputeDays(d2,d1)%7;
switch(weekday){
case MON:res=(res+1)%7;break;
case TUE:res=(res+2)%7;break;
case WED:res=(res+3)%7;break;
case THU:res=(res+4)%7;break;
case FRI:res=(res+5)%7;break;
case SAT:res=(res+6)%7;break;
case SUN:res=(res+7)%7;break;
}
return res;
}

/*
*程序没有进行错误检查
*/
int main()
{
Date d1,d2;
int week,res;
printf(
"enter a date:year month day=======");
scanf(
"%d%d%d",&d1.year,&d1.month,&d1.day);
printf(
"week ?");
scanf(
"%d",&week);
printf(
"enter another date but don't know on which weekday======");
scanf(
"%d%d%d",&d2.year,&d2.month,&d2.day);
res
=Mod7(d1,d2,week);
printf(
"it's ");
switch(res)
{
case MON:printf("MON\n");break;
case TUE:printf("TUE\n");break;
case WED:printf("WED\n");break;
case THU:printf("THU\n");break;
case FRI:printf("FRI\n");break;
case SAT:printf("SAT\n");break;
case SUN:printf("SUN\n");break;
}
return 0;

}