题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <iostream> using namespace std; //平年每一个月的天数 int data1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年每一个月的天数 int data2[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool check(int x) { //用来判断年是不是闰年 if ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0)) { return true; } return false; } int main() { int year, month, day; while (cin >> year >> month >> day) { // 注意 while 处理多个 case int ans = 0; if (check(year)) { for (int i = 0; i < month - 1; i++) { ans += data2[i]; } ans += day; } else { for (int i = 0; i < month - 1; i++) { ans += data1[i]; } ans += day; } cout << ans; } } // 64 位输出请用 printf("%lld")