#33734: C++ 找閏年不用for版


chayno (chayno)

學校 : 國立花蓮高級中學
編號 : 175739
來源 : [111.243.215.208]
最後登入時間 :
2024-03-20 03:08:50
a263. 日期差幾天 -- 板橋高中練習題 | From: [111.243.169.188] | 發表日期 : 2023-01-28 20:03

#include <bits/stdc++.h>
using namespace std;

vector<int> mtod={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 

int before(int y){
    return y*365+y/4-y/100+y/400;
}

int thisyear(int y, int m, int d){
    int ds(d);
    for(int i=1; i<=m-1; i++) ds+=mtod[i];
    if((y%4==0 and (y%100 or y%400==0)) and m>2) ds++;
    return ds;
}

signed main(){
    
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    
    int y1, y2, m1, m2, d1, d2;
    while(cin>>y1>>m1>>d1>>y2>>m2>>d2){
        int rd1, rd2;
        rd1=before(y1-1)+thisyear(y1, m1, d1);
        rd2=before(y2-1)+thisyear(y2, m2, d2);
        cout<<abs(rd1-rd2)<<'\n';
    }
    
    return 0;
}

 
ZeroJudge Forum