Created
December 8, 2014 16:34
-
-
Save amoshyc/f3bea27dc33362098657 to your computer and use it in GitHub Desktop.
uva11356.c
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> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| const char* month[13] = {"", "January", "February", "March", "April", "May", | |
| "June", "July", "August", "September", "October", "November", "December"}; | |
| int is_leap_year(int year) { | |
| if (year % 400 == 0) | |
| return 1; | |
| if (year % 100 == 0) | |
| return 0; | |
| if (year % 4 == 0) | |
| return 1; | |
| return 0; | |
| } | |
| int get_month(const char* s) { | |
| int i; | |
| for(i=1; i<=12; i++) | |
| if (strcmp(s, month[i]) == 0) | |
| return i; | |
| return -1; | |
| } | |
| void print_new_date(int y, int m, int d, int offset) { | |
| int new_y = y; | |
| int new_m = m; | |
| int days_in_year = ((is_leap_year(new_y)) ? 366: 365); | |
| while (offset > days_in_year) { | |
| offset = offset - days_in_year; | |
| new_y++; | |
| days_in_year = ((is_leap_year(new_y)) ? 366: 365); | |
| } | |
| int days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
| if (is_leap_year(new_y)) | |
| days_in_month[2] = 29; | |
| int i, sum = 0; | |
| for(i=1; i<=12; i++) { | |
| if (sum < offset && offset <= sum + days_in_month[i]) { | |
| offset = offset - sum; | |
| new_m = i; | |
| break; | |
| } | |
| sum = sum + days_in_month[i]; | |
| } | |
| printf("%d-%s-%02d", new_y, month[new_m], offset); | |
| } | |
| int main() { | |
| int T; | |
| scanf("%d", &T); | |
| int case_; | |
| for(case_=0; case_<T; case_++) { | |
| char inp[100]; | |
| scanf("%s", inp); | |
| char* year = strtok(inp, "-"); | |
| char* month = strtok(NULL, "-"); | |
| char* date = strtok(NULL, "-"); | |
| int offset; | |
| scanf("%d", &offset); | |
| int y = atoi(year); | |
| int m = get_month(month); | |
| int d = atoi(date); | |
| int days_in_month[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; | |
| if (is_leap_year(y)) | |
| days_in_month[2] = 29; | |
| int i; | |
| for(i=1; i<m; i++) | |
| offset += days_in_month[i]; | |
| offset += d; | |
| printf("Case %d: ", case_+1); | |
| print_new_date(y, m, d, offset); | |
| printf("\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment