题解 | 日期差值
日期差值
https://www.nowcoder.com/practice/ccb7383c76fc48d2bbc27a2a6319631c
#include <iostream> #include <ctime> using namespace std; int daysBetweenDates(struct tm date1, struct tm date2) { time_t t1 = mktime(&date1); time_t t2 = mktime(&date2); double diff = difftime(t2, t1) / (60 * 60 * 24); return static_cast<int>(diff); } int main() { struct tm date1 = {0}; struct tm date2 = {0}; int input1; cin >> input1; date1.tm_year = input1 / 10000-1900; date1.tm_mon = (input1 / 100) % 100-1; date1.tm_mday = input1 % 100; int input2; cin >> input2; date2.tm_year = input2 / 10000-1900; date2.tm_mon = (input2 / 100) % 100-1; date2.tm_mday = input2 % 100; int result = daysBetweenDates(date1, date2); cout << result+1 << endl; return 0; }