[cpp] /********************************* * 日期:2013-1-11 * 作者:SJF0115 * 題號: 九度1043 * 題目:Day of Week * 來源:http://ac.jobdu.com/problem.php?pid=1043 * 結果:AC * 題意: * 總結: **********************************/ #include <stdio.h> #include <string.h> 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, }; char monthName[13][20] = { " ", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; char weekName[8][20] = { " ", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; //判斷閏年 int leap_year(int year){ if((year%100 != 0 && year%4 == 0) || (year % 400 == 0)){ return 1; } else{ return 0; } } //計算天數 int TDay(int day,char month[],int year){ int day1 = 0,i,j; int isleap_year = leap_year(year); for(i = 1;i< year;i++){ if(leap_year(i)){ day1 += 366; } else{ day1 += 365; } } for(i = 1;i < 13;i++){ if(strcmp(month,monthName[i]) == 0){ break; } } for(j = 1;j < i;j++){ day1 += dayOfMonth[j][isleap_year]; } day1 += day; return day1%7+1; } int main() { int day,year; char month[20]; //freopen("C:\\Users\\SJF\\Desktop\\acm.txt","r",stdin); while(scanf("%d%s%d",&day,month,&year) != EOF) { printf("%s\n",weekName[TDay(day,month,year)]); } return 0; }