日期倒计时
题目描述在经济、科技日益发达的今天,人们对时间的把握越来越严格,对于一个一定影响力的公司的高管来说,他可能要将自己的行程提前安排到下个月。对于普通人来说,他也可能将几天之后的安排已经提前做好。
请设计一个程序计算出今天距离未来的某一天还剩多少天。
假设今天是2015年10月18日。
输入
输入一个日期格式为yyyy-MM-dd,不考虑日期是否小于今天。
样例输入
2015-10-19
输出
输出一个数字表示今天(2015年10月18日)距离该日期还剩多少天。
样例输出
1
时间限制 C/C++语言:1000MS 其它语言:3000MS 内存限制 C/C++语言:65536KB 其它语言:589824KB #include<iostream> #include<cmath> #include <map> #include <algorithm> #include <iomanip> #include <cstring> #include <ctype.h> using namespace std; #include<iostream> using namespace std; int monthList[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool isLeapYear(int year) { return ((year0)&&!(year%4))||!(year@0); } int getLeftDates(int year, int month, int date) { int dateCount = 0; for(int i = 2015; i < year; i++) { if(isLeapYear(i)) dateCount += 366; else dateCount += 365; } for(int i = 1; i <= month; i++) { if(isLeapYear(year) && i == 2) dateCount += 29; else dateCount += monthList[i - 1]; } dateCount += date; return dateCount; } int main() { int year, month, date; char c; cin >> year >> c >> month >> c >> date; int leftDatesStart = getLeftDates(2015, 10, 18); int leftNowDates = getLeftDates(year, month, date); int leftDates = leftNowDates - leftDatesStart; cout << leftDates << endl; return 0; }