Search Here

Friday, October 30, 2015

UVa - 12439 - February 29 Solution [ Methods to Solve in C ]

=>Try Yourself First. For help Scroll Down .

It's been weeks since I've posted my Last post in the Site . Today I am going to be posted an easy but bit Tricky problem of UVa . It can be Solved by both C & C ++ . Although my code in C++ format , it can easily be understandable by 'C' language solvers. Here I go.

Clearance :

The problem said to find the number of Leap years between a Range . Watch the TIME LIMIT . Its about 1 sec . If you iterate loop from the beginning to the end Your result will get a TLE . That's why you must find a Naive Approach . There is O(1) solution for this Problem . As like you did to Check whether a Leap year or NOT here you have to do the same thing . You know that a Leap comes after 4 Years later . So, find the number of Multiples of 4 between two range Dividing by 4. Then As you did for Checking Leap Year find the Multiples of 100 . Further find the the Multiples of 400 . Then apply the Rule given Below -
Number of Leap Years = Multiples of 4 - Multiples of 100 + Multiples of 400. Where,  
  • Multiples of 4 = ( Ending Year - (Starting Year -1) ) /4  
  •  Multiples of 100 = ( Ending Year -( Starting Year - 1 ) ) /100 
  •  Multiples of 400 = ( Ending Year - (Starting Year -1 ) ) /400 
The theory behind the Rule is - In checking a year is Leap Year or NOT you know Leap years are always Evenly Divisible by 4 but Not evenly divisible by 100 and Evenly divisible by 400 . So, You actually did Here :
Find out Leap Years : From 0 - Starting Year. 
Find out Leap Years : From 0 to Ending Year. 
Subtract One from Another .
Facts To Check :

There is some special Point you have to check . Those are : 

  • You have to Check out whether the Two given Years are applicable ( Needed or Not ) for your Calculation or Not . 
  • For Example ( For Start/1st date ) : If Start date Said " March 1, 2000 " . As you find the Leap Year and you have to do Calculation with the month "February" you have to skip ( go to next year ) this year . Again If the Starting date is February 29, 2000 then you have to keep it for Calculation . Overall , you have just to check which month do you get is greater than February or Not . If it is then increase by 1 if not then Keep it same.
  • For Example ( For End/2nd date ) : If the date is Less then "February 29 " then Decrease the Year by one's .
  • Take the Inputs Carefully and Handle the output System Carefully .

-----------------------------------------------------------Hope You Solved It Already-----------------------------------------------------------------

Code:

#include<bits/stdc++.h>

using namespace std;

bool isleap(long long int year)
{
    if(((year%4==0)&&(year%100!=0))||(year%400==0)) return true;
    return false;
}

int main()
{
    long long int test, day, day2, year, year2;
    scanf("%lld", &test);
    char date[100], koma, koma2, month[100], date2[100], month2[100];
    getchar();
    for(int caseno = 1; caseno<=test; caseno++)
    {
        gets(date);
        sscanf(date, "%s %lld%c %lld", month, &day, &koma, &year);
        gets(date2);
        sscanf(date2, "%s %lld%c %lld", month2, &day2, &koma2, &year2);

        if(strcmp(month,"January")!=0 && strcmp(month,"February")!=0) year++;
        if(strcmp(month2,"January")==0 || (strcmp(month2,"February")==0 && day2<29)) year2--;
        int four = (year2/4) - ((year-1)/4);
        int hund = (year2/100) - ((year-1)/100);
        int fourhund = (year2/400) - ((year-1)/400);

        int res = four - hund + fourhund;
        printf("Case %d: %d\n", caseno, res);
    }
    return 0;
}




=>Question or Need Help ?? Leave a Comment .

No comments:

Post a Comment