纯水题关键是得处理好闰年、闰月问题。
AC代码:
#include <iostream> using namespace std; int d1[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //平年 int d2[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //闰年 int y, d, m; bool judge(int year){ if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) return true; return false; } int main() { int T; scanf("%d", &T); while (T--){ int i = 0; int res = 0; scanf("%d-%d-%d", &y, &m, &d); if (d == 29){ if (!judge(18 + y)){ printf("-1\n"); continue; } } for (int j = 0; j < m - 1; j++) //减去出生年的天数 { if (judge(y)) res -= d2[j]; else res -= d1[j]; } res -= d;//出生日期减去 while (i < 18){ if (judge(y)) res += 366; else res += 365; y++; i++; } for (int i = 0; i < m - 1; i++){ if (judge(y)) res += d2[i]; else res += d1[i]; } res += d; printf("%d\n", res); } return 0; }
HDOJ 1201 18岁生日
最新推荐文章于 2020-02-13 19:03:04 发布