蓝桥杯训练 日期计算
生活随笔
收集整理的這篇文章主要介紹了
蓝桥杯训练 日期计算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這道題做起來還是有些麻煩的,做了一個小時多點,碼力不行呀。。。
思路:
首先輸入a的年月日,b存儲2011,11,11,交換兩個日期變量,讓日期a 存儲較小的日期,日期b存儲較大的。
?我們需要計算出兩個日期之間相差多少天,我想的是計算 a日期所在年過去了多少天sum1,再計算b日期所在年還有多天沒過sum2,最后用a的年份到b的年份的總天數-(sum1+sum2),得到兩個日期差了多少天。
最后還得分類一下兩個日期是否交換過,有無交換過處理不同。。注釋有。。
代碼
#include <iostream> #include <malloc.h> #include <cstdio> #include <algorithm> #include <queue> #include <cstring> using namespace std; typedef long long LL; const int maxn = 1000 + 10; const int inf = 0x3f3f3f3f; int c[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; struct Data {int year, month, day; }; bool check(int n) {if ((n % 100 != 0 && n % 4 == 0) || (n % 100 == 0 && n % 400 == 0))return true;return false; } int main() { #ifdef ONLINE_JUDGE #elsefreopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout); #endifios::sync_with_stdio(0);cin.tie(0);cout.tie(0);// 已知 2011 年 11 月 11 日是星期五Data a; // a 留小的Data b; // b 留大日子int flag = 0;int ans;b.year = 2011;b.month = 11;b.day = 11;cin >> a.year >> a.month >> a.day;if ((a.year > b.year) || (a.year == b.year && a.month > b.month) || (a.year == b.year && a.month == b.month && a.day > b.day)){swap(a, b);flag = 1;}// cout << a.year << " " << a.month << " " << a.day << endl;// cout << b.year << " " << b.month << " " << b.day << endl;int sum = 0;for (int i = 1; i < a.month; i++){sum += c[i];if (i == 2 && check(a.year))sum++;}sum += a.day;for (int i = b.month; i <= 12; i++){sum += c[i];if (i == 2 && check(b.year))sum++;}sum -= b.day;int all = 0;for (int i = a.year; i <= b.year; i++){all += 365;if (check(i))all++;}all -= sum;if (flag == 0) // 如果沒交換過說明 給定日期比2011 11 11 日期早{ //不能向前找,還是得向后找,具體處理如 73 74 行all = all % 7;ans = (5 + (7 - all)) % 7;if (ans == 0)cout << 7;elsecout << ans;}else{ans = (5 + all) % 7;if (ans == 0)cout << 7;elsecout << ans;}return 0; }總結
以上是生活随笔為你收集整理的蓝桥杯训练 日期计算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TimestampUtil时间处理工具类
- 下一篇: C++ : Hello, World!