POJ3393-Lucky and good months by Gregorian Calendar

Source: Internet
Author: User

 

Reprinted please indicate the source: Thank you

Http://user.qzone.qq.com/289065406/blog/1307664479

 

General question:

In a popular science article, Article 80% is useless information, because it is common sense, but it has to be read, because there are 20% historical common sense that ordinary people do not know.

 

Definition:

Goog month: The month of Monday on the first business day of the month

Luckly month: the last business day of the month is the month of Friday.

Q: Given a Gregorian CalendarClosed Time Interval (that is, the year and month of the endpoint)

[Start year, month ]~ [End year and month]

In this time range, how many Goog month and how many luckly month

 

Key points of the article:

Gregorian Calendar

GC starts from January 1, January 1 and is Saturday.

GC has 365 days on a normal year and 366 days on a leap year (more than one day on January 1, February)

GC has 12 months. The number of days in each month is the same as that in the current calendar.

GC before January 1, 1582 (excluding 1582). If the year can be divisible by four, it is a leap year.

GC after 1582 (including 1582), the criteria for determining a leap year (meeting any of the following criteria ):

(1) It can be divisible by four, but cannot be divisible by 100;

(2) can be divisible by 400.

Due to historical reasons, GC sets 1700 to be an unconditional leap year.

Due to historical reasons, GC rules for the period from January 1, September 3, 1752 ~ There were 11 days in total on the 13th, that is, only 19 days on January 1, September 1752.

GC has 7 days in a week, which are sorted as sun, Mon, Tue, wed, Thu, Fri, SAT, which is the same as the current week, where Mon to Fri is the workday, sun and sat are day off

 

Solution:

It's okay to directly simulate it.

 

First, a leap () function is used to determine a leap year ()

 

Input time rangeStart Year Sy month SmAndEnd year ey month emAfter

Calculate the number of days from January to January, Sy (if Sm = 1, then to December, SY-1)

Note that the number of days of the day is calculated as the last day of the sm-august.

If day + 1, the system enters the input time range [start year, month ]~ The first day of the End year and month

Note the following when calculating day:

(1) The leap year judgment standard has changed around 1582.

(2) 1700 unconditional leap year

(3) 11 days earlier than January 1, September 1752

 

Determine the day of the week:

Because January 1 is Saturday, there are seven days in a week,

Therefore(Day + 5) % 7 can calculate the day of the week.

Day % 7 cannot be used directly, and day % 7 is the default value of Sunday, January 1, January 1. It is not difficult to deduce why + 5 should be used first. Readers can understand it by themselves.

 

Determine if a month is good month or luckly month:

After the day is calculated, make day ++ enter the 1st day of SM.

In this case, judge whether the day 1 of SM is good month and use the method given above to calculate the day of the week. If

If the value 1 is Sunday (0), Saturday (6), or Monday (1), the month is good month.

Starting from the SM month, the number of days day increases by month, and the method for judging whether the month is good month is the same.

It is not difficult to find that if the K-month is good month, the K-month must be luckly month. Therefore, you can add both counters.

 

NOTE 3:

(1) Boundary: If SM is good month, the counter is good ++, but the counter luck remains unchanged, because sm-january 1, January is not in the time range. If the calculated em value is good month, the counters luck ++ and good remain unchanged, because day is plus 1 at the beginning. When day increments to Em month by month, in fact, Day is the first day of the EM + period. In this case, it is determined whether the EM + February is good month. If so, the EM month is luckly month, however, the EM + February is not included in the counter within the time range.

(2) If the value increases by one month, it will take 11 days if the value reaches September 1752.

(3) The number of days of the month in the year of a leap is different.

 

 

 

 

 

// Memory time // 216 K 0 Ms # include <iostream> using namespace STD; int month [13] =, 31}; // year-on-year int lmonth [13] = {0, 31, 29,31, 30,31, 30,31, 31,30, 31,30, 31}; // leap year-month Enum week {sun, Mon, tue, wed, Thu, Fri, sat}; // weekly bool LEAP (INT year); // judge whether year is a leap year int computeday (INT y, int m ); // calculate the total number of days + 1 (that is, only the first day of the M-month) from January 1, 1st to mm-june, Y. Int main (int I, Int J, int * pm) {int test; CIN> test; For (int t = 0; t <test; t +) {Int ys, MS, ye, me; CIN> ys> MS> ye> me; int luck = 0, good = 0; int day = computeday (ys, MS); If (day + 5) % 7 <= Mon) | (day + 5) % 7 = SAT )) // calculate the day of the week in the MS month of YS, and determine whether it is good monthgood ++. // The default value is Sunday, therefore, + 5 Adjustment/* calculates whether the month from Ys year MS + 1 to ye-1 year is good month */for (I = ys; I <ye; I ++) {If (LEAP (I) PM = lmonth; elsepm = month; if (I = ys) J = MS; elsej = 1; for (; j <= 12; j ++) {day + = * (PM + J); if (I == 1752 & J = 9) Day-= 11; // because day starts with + 1, when J is used, whether or not the day J + January is a go Od monthif (day + 5) % 7 <= Mon) | (day + 5) % 7 = SAT) // calculates the day of the week for J + June, determine whether the value is good month {good ++; luck ++; // when J + February is good month, month J must be luck month }}/ * calculate the good month */If (LEAP (I) PM = lmonth; elsepm = month; if (I = ys) // If ye = ysj = MS; elsej = 1; for (; j <= Me; j ++) {day + = * (PM + J); if (I = 1752 & J = 9) Day-= 11; // because day starts at + 1, when J is used, whether the J + February is good monthif (day + 5) % 7 <= Mon) | (day + 5) % 7 = SAT) // calculate the day of the week for J + June, and determine whether it is good month {If (J! = Me) Good ++; luck ++; // when J + January is good month, the month of J must be luck month }}cout <luck <''<Good <Endl;} return 0;} bool LEAP (INT year) // determine whether year is a leap year {If (year <1582) return! (Year % 4); // when year <1582, as long as it can be divisible by 4, it is a leap year else {If (year = 1700) // when year = 1700, for historical reasons, the return true is unconditional for a leap year; if ((! (Year % 4) & (Year % 100) |! (Year % 400) // when year> = 1582, it can be divisible by 4 and not divisible by 100 to return true for a leap year; // or return false for a leap year when the value can be divisible by 400;} int computeday (INT y, int m) // calculate the total number of days from January 1, 1st to mm-june, Y + 1 (that is, only the first day of the MTH month) {// "+ 1" is used to change from the end of the current month to the beginning of next month int I, j, Day = 1; for (I = 1; I <Y; I ++) if (LEAP (I) day + = 366; elseday + = 365; For (j = 1; j <m; j ++) if (LEAP (I )) day + = lmonth [J]; elseday + = month [J]; If (Y> 1752) Day-= 11; else if (y = 1752 & M> 9) day-= 11; return day ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.