304B - Calendar - CodeForces Solution


brute force implementation *1300

Please click on ads to support us..

Python Code:

import datetime
t1=datetime.datetime.strptime(input(),'%Y:%m:%d')
t2=datetime.datetime.strptime(input(),'%Y:%m:%d')
print(abs((t2-t1).days))

C++ Code:

#include<bits/stdc++.h>
#include<iomanip>
#include<string>
#include <vector>
#include<algorithm>
#include <cmath>
#include<cstring>
using namespace std;
#define ll long long
#define gcd(a,b)  __gcd(a,b)
#define MOD ll(1e9+7)
#define ld long double
#define rep(i,x,y) for(ll i=x;i<y;i++)
#define rrep(i,x,y) for(ll i=x;i>=y;i--)
#define max2(a,b) ((a<b)?b:a)
#define max3(a,b,c) max2(max2(a,b),c)
#define min2(a,b) ((a>b)?b:a)
#define min3(a,b,c) min2(min2(a,b),c)
#define fast  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define cy  cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define pb push_back
#define mll map<ll,ll>
#define pii pair<ll,ll>
void solve()
{ ll d=0;
  string s1,s2;
    cin>>s1>>s2;
    int n=10;
    n = s1.length();
    string a1,a2,a3,b1,b2,b3;
    rep(i,0,4)
    {
        a1 += s1[i];
    }
    rep(i,5,7)
    {
        a2 += s1[i];
    }
    rep(i,8,10)
    {a3 += s1[i];}
    int y1 = stoi(a1);
    int m1 = stoi(a2);
    int d1  = stoi(a3);
    rep(i,0,4)
    {
        b1 += s2[i];
    }
    rep(i,5,7)
    {
        b2 += s2[i];
    }
     rep(i,8,10)
    {b3 += s2[i];}
    int y2 = stoi(b1);
    int m2 = stoi(b2);
    int d2 = stoi(b3); 
    if(y1==y2)
    {   ll days =0;
        if(m1==m2)
        {
            days = abs(d2-d1);
            cout<<days;
        }


        else{
          if(m1>m2)
          {swap(m1,m2);
          swap(d1,d2); }           
        for(int i=m1+1;i<m2;++i)
        {
            if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            days += 31;
 
         }
         else if(i==4 || i==6 || i==9 || i==11)
            {days += 30;}
        else if(i==2)
        {
            if(y2%4==0)
                {days += 29;}
            else
                {days += 28;}
        }
        }
        if(m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12)
         {
            days += 31-d1;
 
         }
         else if(m1==4 || m1==6 || m1==9 || m1==11)
            {days += 30-d1;}
        else if(m1==2)
        {
            if(y2%4==0)
                {days += 29-d1;}
            else
                {days += 28-d1;}}
             
            days += d2-1;
            cout<<days+1<<endl;
             
            


    }
    }
else{
    if(y1>y2)
    {
        swap(y1,y2);
        swap(d1,d2);
        swap(m1,m2);
    }
    ll days=0;
     
    for(int i = y1+1;i<=y2-1;++i)
    {
        if(i%4==0)
            {days+= 366;}
        else
            { days += 365;}
    }
     
    
    for(int i=1;i<m2;++i)
    {
         if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            days += 31;
 
         }
         else if(i==4 || i==6 || i==9 || i==11)
            {days += 30;}
        else if(i==2)
        {
            if(y2%4==0)
                {days += 29;}
            else
                {days += 28;}
        }
    }
    days += d2-1;
     
 
    for(int i=1;i<m1;++i)
    {
        if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12)
         {
            d += 31;
 
         }
         else if( i==4 || i==6 || i==9 || i==11)
            {d += 30;}
       
        else if(i==2)
        {
            if(y1%4==0 && y1!=1900)
                {d+= 29;}
            else
                {d += 28;}
        }
         
    }
 
    d += d1;
     if(y1%4!=0 || y1==1900)
    {days += 365-d;}
else
{
         days += 366-d;
}
    cout<<days+1<<endl;}

    
     
 
 
   
      
     
}
int main()
 {     fast;
 ll t=1;
 //cin>>t;
 while(t--)
 { solve();
        
 }
 return 0;}


Comments

Submit
0 Comments
More Questions

1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training