Chinaunix首页 | 论坛 | 博客
  • 博客访问: 25051
  • 博文数量: 4
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 45
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-22 19:19
文章分类
文章存档

2019年(1)

2018年(1)

2017年(2)

我的朋友

分类: C/C++

2017-06-07 23:15:30

偶然的机会看到了一个计算星期几的算法,感觉很怪异并且很有意思就记录一下,也与大家共勉。自己写的代码有些瑕疵,但重点是计算星期的算法值得思索,有不正确的地方希望多多指正。

问题:输入年月日计算是星期几?

代码如下:

#include
int getWeek(int year, int month, int day)
{
int aWeek[12] = {0,3,2,5,0,3,5,1,4,6,2,4};
int week = 0;
int temp = 0;

if(year < 1 || month < 1 || month > 12 || day < 1 || day > 31)
{
return 0;
}
year -= month < 3;
temp = (year + year/4 - year/100 + year/400 + aWeek[month-1] + day) % 7;
switch(temp)
{
case 0: week = 7;break;
case 1: week = 1;break;
case 2: week = 2;break;
case 3: week = 3;break;
case 4: week = 4;break;
case 5: week = 5;break;
case 6: week = 6;break;
default: week = 0;break;
}
return week;
}
int main()
{
int year = 0;
int month = 0;
int day = 0;
int week = 0;
while(1)
{
printf("Please input date:(year month day)\n");
scanf("%d %d %d", &year, &month, &day);
week = getWeek(year, month, day);
if(week < 1 || week > 7)
return 0;
printf("date:%d-%d-%d is %d of week\n", year, month, day, week);
}
return 0;
}

阅读(1606) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:单链表的头插法和尾插法c语言实现

给主人留下些什么吧!~~