/** * MIDI Olympiad in Informatics 2007 * Personal Code reference solution * by Adomas Paltanavicius */ #include #include #define OK 1 #define FAIL 0 const char infile[] = "pcode.in"; const char outfile[] = "pcode.out"; char coeff1[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 }; char coeff2[10] = { 3, 4, 5, 6, 7, 8, 9, 1, 2, 3 }; int all_month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; char code[11]; int year, month, day; /* Output answer to file. */ void answer(int ok) { freopen(outfile, "w", stdout); printf("%s\n", ok ? "OK" : "FAIL"); exit(0); } /* Calculate checksum. */ int checksum() { int sum, i; for (i = sum = 0; i < 10; i++) { sum += coeff1[i] * code[i]; } if (sum % 11 == 10) { for (i = sum = 0; i < 10; i++) { sum += coeff2[i] * code[i]; } if (sum % 11 == 10) { sum = 0; } } return sum % 11; } void validate_date() { /* First, validate month and day. Then, * look if date is not in future. */ int year_leap = year%4==0 && (year%100!=0 || year%400==0); int month_days; /* Check month. */ if (month < 1 || month > 12) answer(FAIL); /* Check day. */ month_days = all_month_days[month - 1]; if (month == 2 && year_leap) month_days++; if (day < 1 || day > month_days) answer(FAIL); /* No later than 2007 april 28th. */ if (year > 2007 || (year == 2007 && (month > 4 || (month == 4 && day > 28)))) { answer(FAIL); } } /* Check code. */ void check() { /* Get century */ if (code[0] > 0) { year = 1700 + 100 * (code[0] + 1) / 2; } else { answer(FAIL); } year += code[1] * 10 + code[2]; month = code[3] * 10 + code[4]; day = code[5] * 10 + code[6]; validate_date(); if (code[10] != checksum()) { answer(FAIL); } else { answer(OK); } } int main() { int i; freopen(infile, "r", stdin); scanf("%11s", code); /* Convert to number. */ for (i = 0; i < 11; i++) code[i] -= '0'; check(); return 0; }