题解 | 日期差值

日期差值

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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-10 11:33
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务