题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <stdio.h> int is_years(int y) { if (y % 4 == 0 && y % 100 != 0) { return 1; } if (y % 400 == 0) { return 1; } return 0; } int main() { int y = 0; int m = 0; int d = 0; int ret = 0; int days[13] = {0,31,0,31,30,31,30,31,31,30,31,30,31}; while (scanf("%d %d %d", &y, &m, &d) != EOF) { // 注意 while 处理多个 case // 64 位输出请用 printf("%lld") to if (is_years(y)) { days[2] = 29; } else { days[2] = 28; } for (int i = 1; i < m; i++) { ret += days[i]; } ret += d; } printf("%d\n", ret); return 0; }