1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| #include <stdio.h>
#define SMALL (365 * 3 + 366) #define MID (SMALL * 25 - 1) #define BIG (MID * 4 + 1)
char* pszWeekdays[7] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
int nDaysOfMon[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };
void GetMonthAndDay(int nDays, bool bIsLeap, int* nMon, int* nDay) { int nChoose = 0; int i = 0;
if (bIsLeap) { nChoose = 1; } *nMon = *nDay = 0; i = 1; while (nDays > nDaysOfMon[nChoose][i]) { nDays -= nDaysOfMon[nChoose][i]; ++i; } *nMon = i; *nDay = nDays; }
void CountSmall(int* pnDays, int* pnPastYears, int* pnMon, int* pnDay) { if (*pnDays >= 3 * 365) { *pnPastYears += 3; *pnDays -= 365 * 3; GetMonthAndDay(*pnDays + 1, true, pnMon, pnDay); } else { *pnPastYears += *pnDays / 365; *pnDays %= 365; GetMonthAndDay(*pnDays + 1, false, pnMon, pnDay); } }
int main() { int nPastDays = 0; int nPastYears = 0; int nYear = 0, nMon = 0, nDay = 0;
while (scanf("%d", &nPastDays), nPastDays != -1) { int nTemp = nPastDays; nPastYears = 0;
if (nTemp < 366) { GetMonthAndDay(nTemp + 1, true, &nMon, &nDay); nPastYears = 0; } else { nPastYears++; nTemp -= 366;
nPastYears += (nTemp / BIG) * 400; nTemp %= BIG;
if (nTemp >= MID * 3) { nTemp -= MID * 3; nPastYears += 300;
nPastYears += nTemp / SMALL * 4; nTemp %= SMALL;
CountSmall(&nTemp, &nPastYears, &nMon, &nDay); } else { nPastYears += nTemp / MID * 100; nTemp %= MID;
if (nTemp >= SMALL * 24) { nTemp -= SMALL * 24; nPastYears += 4 * 24;
nPastYears += nTemp / 365; nTemp %= 365; GetMonthAndDay(nTemp + 1, false, &nMon, &nDay);
} else { nPastYears += nTemp / SMALL * 4; nTemp %= SMALL;
CountSmall(&nTemp, &nPastYears, &nMon, &nDay); } } } printf("%d-%02d-%02d %s\n", nPastYears + 2000, nMon, nDay, pszWeekdays[nPastDays % 7]); }
return 0; }
|