Last active
December 18, 2016 12:01
-
-
Save neoatlantis/1a489ab5d7e03d7860840fb85e25db56 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
struct Date{ | |
int year; | |
int month; | |
int day; | |
}; | |
int dateDiff(struct Date, struct Date); | |
int yearDays(int); | |
int monthDays(int, int); | |
int whichWeekday(struct Date); | |
int isLeapYear(int); | |
int print(int); | |
int main(){ | |
int n, i, results[1000]; | |
struct Date date; | |
scanf("%d", &n); | |
for(i=0; i<n; i++){ | |
scanf("%d %d %d", &date.year, &date.month, &date.day); | |
results[i] = whichWeekday(date); | |
} | |
for(i=0; i<n; i++){ | |
print(results[i]); | |
} | |
} | |
int print(int wday){ | |
char *t[] = { | |
"Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat." | |
}; | |
printf("%s\n", t[wday]); | |
} | |
int whichWeekday(struct Date d){ | |
struct Date compare = { | |
.year = 2016, | |
.month = 12, | |
.day = 18 | |
}; | |
int cweekday = 0; // sunday | |
int diff = dateDiff(d, compare); | |
int offset, ret; | |
offset = diff % 7; | |
ret = cweekday + offset; | |
if(ret > 6){ | |
ret -= 7; | |
} | |
if(ret < 0){ | |
ret += 7; | |
} | |
return ret; | |
} | |
int dateDiff(struct Date d1, struct Date d2){ | |
int i; | |
long dd1 = 0, dd2 = 0; | |
for(i=0; i<d1.year; i++) dd1 += yearDays(i); | |
for(i=1; i<d1.month; i++) dd1 += monthDays(d1.year, i); | |
dd1 += d1.day; | |
for(i=0; i<d2.year; i++) dd2 += yearDays(i); | |
for(i=1; i<d2.month; i++) dd2 += monthDays(d2.year, i); | |
dd2 += d2.day; | |
return dd1 - dd2;; | |
} | |
int yearDays(int year){ | |
if(isLeapYear(year)) return 366; | |
return 365; | |
} | |
int monthDays(int year, int month){ | |
if( | |
1 == month || 3 == month || 5 == month || 7 == month || | |
8 == month || 10 == month || 12 == month | |
){ | |
return 31; | |
} else if(2 == month){ | |
return isLeapYear(year) ? 29 : 28; | |
} else { | |
return 30; | |
} | |
} | |
int isLeapYear(int year){ | |
if(0 == year % 400){ | |
return 1; | |
} else if (0 == year % 100){ | |
return 0; | |
} else { | |
return (0 == year % 4); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment