1. 程式人生 > >華為oj中級 計算日期到天數轉換

華為oj中級 計算日期到天數轉換

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<unordered_map>
#include<fstream>
#include<sstream>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<utility>
#include<iomanip>
using namespace std; unsigned int num_day; int a[11] = { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; int b[11] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; bool is_leap(unsigned int Y){ if ((Y % 4 == 0) || ((Y % 100 == 0 )&& (Y % 400 == 0))) return true; return false; } bool
time_to_day(unsigned int y, unsigned int m, unsigned int d){ if (m > 12 || d > 31 ) return false; //判斷平年和閏年 if (is_leap(y)){ if (m == 1){ num_day = d; return true; } else{ num_day = a[m - 2] + d; return true; } } else
{ if (m == 1){ num_day = d; return true; } else{ num_day = b[m - 2] + d; return true; } } } int main() { unsigned int year, month, day; while (cin >> year >> month >> day){ bool flag = time_to_day(year, month, day); if (flag){ cout << num_day << endl; } else{ cout << -1 << endl; } } return 0; }