From "HDU 2005 first few days?" Talk

      In programming, the date and time of treatment often encounter. The following examples or exercises occur in some textbooks in C language programming.

[Example 1] The first few days? (HDU 2005)

      Given a date, output of this date is the first day of the year.

Input
input data multiple groups, each accounting for one line, the data in the format YYYY / MM / DD composition details, see sample input, in addition, can you make sure that all of the input data is legitimate.

Output
For each case, an output line, indicating that the date is the first day of the year.

Sample Input
1985/1/20
2006/3/12

Sample Output
20
71

      (1) 1 programming ideas.

      For the month month, it requires a total of 1 ~ month-1月的各个月份的天数. For example, month equal to 8, the number of days required cumulative July 1, i.e., d = 0 + 31 (month) +28 (or 29, February days) +31 (March) +30 (April) +31 ( may) + 30 (June) +31 (July), the operation cycle can be written as:

         d=0;

         for( i=1; i<=month-1; i++)

              d = d + i days of the month;

      But it may not be written as a loop, with a switch ... case structure to solve. Because, large accumulation of the month must contain a small month, so when scheduling case constant expression can be descending, and after each entrance, do not break out of the switch statement structure, so you can complete the cumulative specific described as:

    d=0;

           switch(month-1)

           {

         case 11:d+=30;

         case 10:d+=31;

         case 9:d+=30;

         case 8:d+=31;

         case 7:d+=31;

         case 6:d+=30;

         case 5:d+=31;

         case 4:d+=30;

         case 3:d+=31;

         case 2: d + = 28 (or d + = 29);

         case 1:d+=31;

      }

      In addition, in the program, it is necessary to determine whether a particular year a leap year, because of leap year February 29 days, leap year February rather than 28 days.

      Leap year determination conditions are: ① divisible by 4 but not divisible by 100 are leap years, such as 1996, 2004 is a leap year; ② be divisible by 100, but the year divisible by 400 is a leap year. As 2000 is a leap year. It can be represented by a logical expression:

            (year%4==0&&year%100! =0) | | year%400==0

      When a year is an integer value, if the above expression evaluates as true (1), then the year is a leap year; otherwise non-leap year.

      (2) a source.

        #include <stdio.h>

int main()
{
      int year,month,day,d;
      while (scanf("%d/%d/%d",&year,&month,&day)!=EOF)
     {
           d=0;
           switch(month-1)
           {
                 case 11:d+=30;
                 case 10:d+=31;
                 case 9:d+=30;
                 case 8:d+=31;
                 case 7:d+=31;
                 case 6:d+=30;
                 case 5:d+=31;
                 case 4:d+=30;
                 case 3:d+=31;
                 case 2:d+=28;
                      if (year%4==0 && year%100!=0 || year%400==0) d++;
                 case 1:d+=31;
           }
           d=d+day;
           printf("%d\n",d);
      }
      return 0;
}

      (3) 2 programming ideas.

      After studied the array, we can be the number of days in each month stored in the array table [13], where the number of days table [i] value of the i-th month. Such accumulation can be completed with one cycle. February 28 to save the default number of days for leap year special treatment.

      Such write programs more concise. In the course of our study, the program should write simple and efficient as their programming style and goals.

      (4) 2 source.

#include <stdio.h>
int main()
{
      int table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
      int year,month,day,d,i;
      while (scanf("%d/%d/%d",&year,&month,&day)!=EOF)
      {
            d=0;
            for (i=1;i<=month-1;i++)
                  d+=table[i];
            if (month>2 && (year%4==0 && year%100!=0 || year%400==0))
                  d++;
            d=d+day;
            printf("%d\n",d);
      }
      return 0;
}

[Example 2] day of the month.      

      Enter a year year and an integer d, d seek year in the first day is a day of the month? For example, enter 200 671, the output should be 312, that in 2006 the first 71 days is March 12.

      (1) programming ideas.

      An inverse process in the program execution. To simplify the design, the program uses a two-dimensional array to hold each non-leap years and leap year days each month. Cyclic first determine how many months before the d-day, minus the number of days each month, it is the remaining few days.

      (2) source.

#include <stdio.h>
int main()
{
       int table[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31}
                                ,{0,31,29,31,30,31,30,31,31,30,31,30,31}};
      int year,month,day,d,leap;
      while (scanf("%d %d",&year,&d)!=EOF)
      {
           month=1;
           if (year%4==0 && year%100!=0 || year%400==0)
                leap=1;
          else
               leap=0;
          while (d>table[leap][month])
          {
                d-=table[leap][month];
                month++;
          }
          day=d;
          printf("%d %d\n",month,day);
      }
      return 0;
}

[Example 3] What day is today.

       Enter a date in years, months, days later, the output of which day of the week.

      (1) programming ideas.

      Set variable year, month and day respectively, year, month and day input. In Example 1 we find a date is the first few days of the year (to d), due seven days a week, so if we know that New Year's Day is the day of the week (to week), then the date is It can be determined the day of the week, is calculated as: (week + d-1)% 7.0 for Sunday.

      How to find reign as the New Year's Day this year is the day of the week it? A simple formula can be calculated.

      It is known to the year 1 January 1 Monday. Do not ask why, calendar compiled starting point is so specified.

      We know that 365 days a year (of course, there will be a leap year 366 days, regardless of the first), one week every 7 days, 365% 7 = 1. That is, without considering the leap year, the annual New Year's Day should be a day of the week after New Year's Day last year, the week day.

      Since New Year's Day 1 is Monday, so New Year's Day 2 Tuesday, 3 Wednesday, New Year's Day, New Year's Day 4 Thursday, that is ...

      week =(year)%7。

      Because of an extra day in February will be a leap year, so a leap year New Year's Day next year plus one day of the week should be, that is, five years Saturday (not Friday because four years is a leap year), so you need to know before year-1年how many leap years. Leap year rule is a leap year every four brief years, not a leap year every 100 years, every 400 years is a leap year. Press set comprising inclusion and exclusion rules, the number of leap years (year-1) / 4 - (year-1) / 100 + (year-1) / 400.

     Consequently, the known reign year, we can calculate the New Year's Day is the day of the week based on year. Calculated as follows:

         week=[ year+(year-1)/4-(year-1)/100+(year-1)/400]%7;

      (2) source.

#include <stdio.h>
int main()
{
      int table[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
      char str[7][3]={"日","一","二","三","四","五","六"};
      int year,month,day,d,i,week;
      while (scanf("%d/%d/%d",&year,&month,&day)!=EOF)
      {
            week=(year+(year-1)/4-(year-1)/100+(year-1)/400)%7;
            d=0;
            for (i=1;i<=month-1;i++)
                d+=table[i];
            if (month>2 && (year%4==0 && year%100!=0 || year%400==0))
                d++;
            d=d+day;
            week=(week+d-1)%7;
            printf("On May% d% d% d day week S% \ n-", year, month The, Day, STR [Week]);       return 0;
      }

}

   

Guess you like

Origin www.cnblogs.com/cs-whut/p/11511301.html