Date and time Issues-calendar issues

Source: Internet
Author: User

Recently the blogger took part in a school quiz, which has a calendar problem that is currently a certain number of days of the day of the week, let you enter a few n, calculate how many years n days is the number of months how many days of the week;

After the Bo Master did not make it, because his method is more complex, the code is more. And then today just with the students to discuss the problem found his method is relatively simple, less code, a total of more than 20 lines of code, here to share the needs of students;


Problem Description:

Suppose it is January 1, 2016 Friday, please enter a number n, programmed to calculate the date n days;

(Input-1 means end, assuming the year does not exceed 9999);


Full code:

#include <iostream>

int judge (int n)
{
if (n% 4! = 0 | | (n% = = 0 && n 400! = 0)) return 0;
return 1;
}


Int Main ()
{
    char week[][20] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "WEDNESDA Y "," Thursday "};
    int  year[] = {365,366};
    int  month[][12] = {31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31} ;
    int days, DayOfWeek, I, J;
    
    while (scanf ("%d", &days) && days >= 0)
    {
&nb Sp       DayOfWeek = days% 7;

        for (i = $; days >= Year[judge (i)]; ++i)
          & nbsp Days-= Year[judge (i)];
        for (j = 0; >= Month[judge (i)][j]; ++j)
          &NBS P Days-= Month[judge (i)][j];

        printf ("%d-%d-%d-%s\n", I, J + 1, days + 1, Week[dayofweek]);
     }

    return 0;
}


This question examines two questions:

1, leap year judgment;

2, the judgment of the week;

Analysis:

1, for the first problem, we can write a function to return the bool type to make the year judgment;

And leap years we all know: can be divisible by 4, or can be divisible by 100 and not divisible by 400 year

There are two ways of doing this:

One

if (n% 4 = = 0) return 1;

Else

if (n%100 = = 0 && n%!! = 0) return 1;

else return 0;

Both

if (n% 4 = = 0 && n%! = 0 | | n = = = 0) return 1;

else return 0;

In fact, these two methods are a meaning, but in the wording of the gap, it is worth noting that we write the order of the judging conditions must be noted, because if it is | | And the && operator, it is prone to short-circuit evaluation, resulting in the subsequent expression is not calculated, resulting in unexpected results;

2, for the second problem is actually very simple;

For example, today's week x, the number of weeks after the calculation should be the initial value of the number of days to 7 to take remembers; (set today is the Week 5, if the number of days is 37, that is the number of 37 to 7 for 2 days, so is 1 weeks six, 2 weeks days, so 37 days later is Sunday);


I personally feel that this code is relatively concise place is that it directly to the leap year non-leap years, each month, the number of weeks, directly initialized to the array, so as to avoid the beginner stage of its constant judgment calculation cumbersome code, and such words are relatively easy to understand.

So after such a similar data or problem arises, it can also be considered, the data is initialized to the array, the key part of the code extracted to write a separate small function, and then need to call directly.


Date and time Issues-calendar issues

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.