日期類問題-判斷星期幾
阿新 • • 發佈:2018-01-13
alt truct urn strcmp int 分享圖片 september sunday while
題目及思路:
代碼:
#include <stdio.h> #include <string.h> int isLeapYear(int x){//1代表閏年 if( (x%4==0 && x%100!=0) || x%400==0) return 1; return 0; } int dayOfMonth[13][2]{ 0,0, 31,31, 28,29, 31,31, 30,30, 31,31, 30,30, 31,31, 31,31, 30,30, 31,31,30,30, 31,31 }; struct Date{ int day; int month; int year; void nextDay(){ day++; if( day > dayOfMonth[month][isLeapYear(year)]){ day=1; month++; if(month > 12){ month=1; year++; } } } };int buf[3001][13][32];//保存天數 char monthName[13][20]{//月名 "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; char weekName[7][20]{//周名 "Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday" }; int abs(int x){ if(x < 0) return -x; return x; } int main(){ Date time; time.day=1; time.month=1; time.year=1000; int cnt=0;//記錄天數 while(time.year!=3001) { buf[time.year][time.month][time.day]=cnt; time.nextDay(); cnt++; } int d,m,y; char s[20]; while( scanf("%d%s%d",&d,&s,&y)!= EOF){ for(m=1;m<=12;m++){ //得出月數 if(strcmp(s,monthName[m])==0) break; } int days=abs( buf[y][m][d] - buf[2018][1][8])+1; printf("%s\n",weekName[days%7]); } return 0; }
日期類問題-判斷星期幾