user-0 提问: C语言 关于日期和星期 大佬救救我

回复:
user: user-1
content: C和C++完整教程:https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程:https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-2
content: 修正8#楼的错误，供参考：#include <stdio.h>  int  get_Week(int y,int m,int d); int  isLeap(int y); int  GetDaysOfMonth(int y, int m); void CountDaysOfCoincide(int *cdofc,int y,int m,int s_d=1,int e_d=0);  int main() {     int i,y,m,year,month,day,year1,month1,day1;     int cdofc[8]; //([0]:总数，[1~7]:周一 ~ 周日)      while (1){            for(i=0;i<8;i++) cdofc[i]=0;            if(scanf("%d%d%d",&year,&month,&day)==EOF)break; //ctrl+Z 结束输入            scanf("%d%d%d",&year1,&month1,&day1);            if((day > GetDaysOfMonth(year,month)) ||(day1 > GetDaysOfMonth(year1,month1))||                          day < 1 || day1<1 || month>12 || month1> 12 || month<1 || month1<1)                printf("Invalid date,input again!\n");            else{                if(year==year1 && month==month1)   //同年同月                    CountDaysOfCoincide(cdofc,year,month,day,day1-1);                else{                    CountDaysOfCoincide(cdofc,year,month,day);                    for(y=year,m=month+1,m>12?(m=1,y++):m;y<=year1 ;m++,m>12?(m=1,y++):m){                        if(y==year1 && m==month1)break;                        CountDaysOfCoincide(cdofc,y,m);                    }                    if(day1!=1)                         CountDaysOfCoincide(cdofc,year1,month1,1,day1-1);                }                for(i=1;i<8;i++)cdofc[0]+=cdofc[i];                printf("%d %d %d %d %d %d %d %d\n",cdofc[0],cdofc[1],cdofc[2],cdofc[3],                                                   cdofc[4],cdofc[5],cdofc[6],cdofc[7]);            }     }     system("pause");     return 0; }  int get_Week(int y,int m,int d) {     int week = 0;     if(m==1 || m == 2){        m += 12;y--;     }     week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;     return week; }  int isLeap(int y) {     if((y%4==0 && y%100!=0)||(y%400==0))         return 1;     else         return 0; }  int GetDaysOfMonth(int y, int m) {     int daysOfMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};     if(isLeap(y) && m == 2) return daysOfMonth[m-1]+1;     return daysOfMonth[m-1]; }  void CountDaysOfCoincide(int *cdofc,int y,int m,int s_d,int e_d)//月的吻合日天数 {     int week = get_Week(y,m,s_d)+1;     int days;     if(e_d == 0)days = GetDaysOfMonth(y,m);     else        days = e_d;     int i    = s_d;     while(i <= days){           if(week == i%10) cdofc[week]++;           week = (week + 1) % 7;           if(week == 0) week =7;           i++;     } }

user: user-2
content: 觉得终止年月日的控制（不含），一段时间范围内的控制，才是真麻烦，大家批评指正，供参考：#include <stdio.h>  int  get_Week(int y,int m,int d); int  isLeap(int y); int  GetDaysOfMonth(int y, int m); void CountDaysOfCoincide(int *cdofc,int y,int m,int d=1);  int main() {     int i,y,m,year,month,day,year1,month1,day1;     int cdofc[8]; //([0]:总数，[1~7]:周一 ~ 周日)      while (1){            for(i=0;i<8;i++) cdofc[i]=0;            if(scanf("%d%d%d",&year,&month,&day)==EOF)break; //ctrl+Z 结束输入            scanf("%d%d%d",&year1,&month1,&day1);            if((day > GetDaysOfMonth(year,month)) ||(day1 > GetDaysOfMonth(year1,month1))||                          day < 1 || day1<1 || month>12 || month1> 12 || month<1 || month1<1)                printf("Invalid date,input again!\n");            else{                if(year==year1 && month==month1)   //同年同月                    CountDaysOfCoincide(cdofc,year,month,day);                else{                    CountDaysOfCoincide(cdofc,year,month,day);                    for(y=year,m=month+1; y<=year1 ;m++,m>12?(m=1,y++):m){                        if(y==year1 && m==month1)break;                        CountDaysOfCoincide(cdofc,y,m);                    }                    if(month1!=1 && day1!=1)                        CountDaysOfCoincide(cdofc,year1,month1);                }                for(i=1;i<8;i++)cdofc[0]+=cdofc[i];                printf("%d %d %d %d %d %d %d %d\n",cdofc[0],cdofc[1],cdofc[2],cdofc[3],                                                   cdofc[4],cdofc[5],cdofc[6],cdofc[7]);            }     }     system("pause");     return 0; }  int get_Week(int y,int m,int d) {     int week = 0;     if(m==1 || m == 2){        m += 12;y--;     }     week=(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;     return week; }  int isLeap(int y) {     if((y%4==0 && y%100!=0)||(y%400==0))         return 1;     else         return 0; }  int GetDaysOfMonth(int y, int m) {     int daysOfMonth[12] = {31,28,31,30,31,30,31,31,30,31,30,31};     if(isLeap(y) && m == 2) return daysOfMonth[m-1]+1;     return daysOfMonth[m-1]; }  void CountDaysOfCoincide(int *cdofc,int y,int m,int d)//月的吻合日天数 {     int days = GetDaysOfMonth(y,m);     int week = get_Week(y,m,d)+1;     int i    = d;     while(i <= days){           if(week == i%10) cdofc[week]++;           week = (week + 1) % 7;           if(week == 0) week =7;           i++;     } }  //2021 2 1 //2021 3 31 //14 2 2 2 2 2 2 2 //^Z //请按任意键继续. . .

user: user-3
content: 90009同学闷头鼓捣小代码，不注意听讲，又退步了~

user: user-4
content: 这题 唯一的难点是 知道一个日期怎么确定是周几 这个其实有固定的数学公式可以计算 比如 蔡勒公式 你查查这种公式然后套进去  就完事了

user: user-4
content: 引用 4 楼 智者知已应修善业 的回复: 2022-3-2 是周三  你又错了哦

user: user-5

user: user-1
content: C和C++完整教程：https://blog.csdn.net/it_xiangqiang/category_10581430.html C和C++算法完整教程：https://blog.csdn.net/it_xiangqiang/category_10768339.html

user: user-0
content: 能否给个明确的代码

user: user-5
content: 可参自适应函数