目录
给定日期,输出对应是星期几
测试结果
如1900年 5月6日是星期三,计算给的日期是星期几
给定日期,输出对应是星期几
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main()
{char input[100];int date[3];int num[12] = {31,28,31,30,31,30,31,31,30,31,30,31};int temp;printf("input data:");while(scanf("%s", input) != EOF){temp = 0;char *p;int num4;int num100;int num400;int sumDay;int isRun;num[1] = 28;p = strtok(input, ",");while(p != NULL && temp < 3){date[temp] = atoi(p);temp++;p = strtok(NULL, ",");}num4 = date[0]/4;num100 = date[0]/100;num400 = date[0]/400;sumDay = (date[0] - 1)*365 + date[2] + num4 - num100 + num400;if((date[0]%4 == 0 && date[0] % 100 != 0) || date[0]%400 == 0)num[1] = 29;for(int i = 0; i < date[1] - 1; i++)sumDay += num[i];switch(sumDay % 7){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;}}return 0;
}
测试结果
如1900年 5月6日是星期三,计算给的日期是星期几
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<math.h>int main()
{int num1900[3];int inputY,inputM,inputD;int sumDay1900;int sumDayInput = 0;int num[12] = {31,28,31,30,31,30,31,31,30,31,30,31};int inputDate[3];int gap;scanf("%d,%d,%d", &inputY, &inputM, &inputD);num1900[0] = (1900 - 1) / 4;num1900[1] = (1900 - 1) / 100;num1900[2] = (1900 - 1) / 400;if((1900/4 == 0 && 1900/100 != 0) || 1900 / 400 == 0)num[1] = 29;//1900年5月6号是星期三sumDay1900 = (1900 - 1) * 365 + num1900[0] - num1900[1] + num1900[2] + 6; //几日在这里直接加上了,下面只加月份for (int i = 0; i < 5; i++)sumDay1900 += num[i];num[1] = 28;if((inputY/4 == 0 && inputY/100 != 0)|| inputY/400 == 0)num[1] = 29;inputDate[0] = (inputY - 1) / 4;inputDate[1] = (inputY - 1) / 100;inputDate[2] = (inputY - 1) / 400;sumDayInput = (inputY - 1)* 365 + inputDate[0] - inputDate[1] + inputDate[2] + inputD;for(int i = 0; i < inputM; i++)sumDayInput += num[i];printf("sumDayInput: %d, sumDay1900: %d inputY: %d inputM: %d inputD: %d\n",sumDayInput, sumDay1900, inputY, inputM, inputD);if(sumDayInput > sumDay1900){gap = sumDayInput - sumDay1900;switch(gap % 7){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;}}else {gap = sumDay1900 - sumDayInput;switch(gap % 7){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;}}return 0;
}