Calendar issues (c + + implementation)

Source: Internet
Author: User

Description: In the calendar that we are using now, the leap year is defined as the year that can be divisible by 4, but the years that can be divisible by 100 and not divisible by 400 are the exception, and they are not leap days. For example: 1700, 1800, 1900, and 2100 are not leap years, while 1600, 2000, and 2400 are leap years. Given the number of days that have elapsed since January 1, 2000 A.D., your task is to give the day of the year and the day of the week.

Input: The input contains several lines, each containing a positive integer representing the number of days that have elapsed since January 1, 2000. Enter the last line is −1, do not need to handle. You can assume that the result will not exceed 9999 years.

Output: For each test sample, output a row that contains the corresponding date and day of the week. The format is "Yyyy-mm-dd DayOfWeek", where "DayOfWeek" must be one of the following: "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" and "Saturday".

Input

1730 1740 1750 1751-1

Output
2004-09-26 Sunday 2004-10-06 Wednesday 2004-10-16 Saturday 2004-10-17 Sunday

Analysis: The difficulty is month and day judgment, we can use such a method to calculate, first calculate the year, the beginning is 2001, the remaining days is N, if n is greater than the number of days in the year, the number of years plus one, and the remaining days minus the number of days, you know the remaining days less than the number of days, you can determine The same method determines the month. The last remaining days plus 1 is the date. The week's judgment was added n%7 on the basis of the week of January 1, 2000.
1#include <iostream>2#include <iomanip>3 using namespacestd;4 5 intYeardays (intYear//returns the number of days in the year6 {7     if(Year%4==0&& Year% -!=0) || Year% -==0)8         return 366;9     ElseTen         return 365; One } A  - intMonthdays (intYearintMonth//returns the corresponding month number of days - { the     Switch(month) -     { -      Case 1:return  to; -      Case 2: +         if(Yeardays (year) = =365) -             return  -; +         Else A             return  in; at      Case 3:return  to; -      Case 4:return  -; -      Case 5:return  to; -      Case 6:return  -; -      Case 7: -      Case 8:return  to; in      Case 9:return  -; -      Case Ten:return  to; to      Case  One:return  -; +      Case  A:return  to; -     } the }                                                     * voidCalintNint&year,int&month,int&day)//Calculate Month Day $ {Panax NotoginsengYear = -; -      while(N >=yeardays (year)) the     { +N-=yeardays (year); Ayear++; the     } +month =1; -      while(N >=monthdays (year,month)) $     { $N-=monthdays (year, month); -month++; -     } theDay = n+1; - }Wuyi voidWeekintN//calculate the day of the week the { -     intWeek = n%7; Wu     Switch(week) -     { About      Case 0: cout <<"Saturday"; Break; $      Case 1: cout <<"Sunday"; Break; -      Case 2: cout <<"Monday"; Break; -      Case 3: cout <<"Tuesday"; Break; -      Case 4: cout <<"Wednesday"; Break; A      Case 5: cout <<"Thursday"; Break; +      Case 6: cout <<"Friday"; the     } - } $  the intMain () the { the     intN,year,month,day; the  -      while(CIN >> N) && (n! =EOF)) in     { the N; the Cal (N, year, month, day); Aboutcout << Year <<"-"&LT;&LT;SETW (2) <<setfill ('0') << Month <<"-"&LT;&LT;SETW (2) <<setfill ('0') << Day <<" "; the week (n); thecout <<Endl; the     } +System"Pause"); -     return 0; the}

Calendar issues (c + + implementation)

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.