判定一个日期是周几,有什么方法?
判定一个日期是周几,有什么方法?
各种系统api
time.h
蔡勒公式
挑一个
#include<stdio.h> int week(int,int); int main() { int year; scanf("%d",&year); for(int i=1; i<13; i++) if(week(year,i)==5) printf("%d年%d月13日是黑色星期五\n",year,i);//注某月的13日是星期五称为黑色星期五. return 0; } int week(int year,int month) { if(month<3) { month+=12; year--; } return (13+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7+1; }谢谢没用蔡的它的要分开年这个简单一点
[此贴子已经被作者于2018-6-6 23:40编辑过]
#include "stdio.h" void CaculateWeekDay(int y,int m, int d) { if(m==1||m==2) { m+=12; y--; } int iWeek=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7; switch(iWeek) { case 0: printf("星期一\n"); break; case 1: printf("星期二\n"); break; case 2: printf("星期三\n"); break; case 3: printf("星期四\n"); break; case 4: printf("星期五\n"); break; case 5: printf("星期六\n"); break; case 6: printf("星期日\n"); break; } } int main() { int year=0,month=0,day=0; printf("请输入日期:\n格式为:1900,1,1\n"); char temp = '1'; while (temp != '0') { scanf("%d,%d,%d",&year,&month,&day); scanf("%c",&temp); CaculateWeekDay(year,month,day); printf("输入0退出,其他继续:"); scanf("%c",&temp); } }