• 计算机考研机试指南(二)——日期类问题


    编程笔记 cha2-2 日期类问题

    日期差值

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #define isRein(x) ((x%100!=0)&&(x%4==0)) || (x%400 ==0)?1:0
     6 using namespace std;
     7 /*
     8     题目:日期差值
     9 
    10 */
    11 int dayOfMonth[13][2] =
    12 {
    13   0,0,// 便于直接从1月开始访问
    14   31,31,
    15   28,29,
    16   31,31,
    17   30,30,
    18   31,31,
    19   30,30,
    20   31,31,
    21   31,31,
    22   30,30,
    23   31,31,
    24   30,30,
    25   31,31
    26 };
    27 
    28 struct Date
    29 {
    30     int Year;
    31     int Month;
    32     int Day;
    33     void nextDay()
    34     {
    35         if (++Day > dayOfMonth[Month][isRein(Year)])
    36         {
    37             Day = 1;
    38             Month ++ ;
    39         }
    40         if (Month>12)
    41         {
    42             Month = 1;
    43             Year ++;
    44         }
    45 
    46 
    47     }
    48 };
    49 int buffer[5000][12][31];
    50 void preProcess()
    51 {
    52     Date tmp ;
    53     tmp.Day = 1;
    54     tmp.Month = 1;
    55     tmp.Year = 0;
    56     int c = 1;
    57     while (tmp.Year < 5000)
    58     {
    59         buffer[tmp.Year][tmp.Month][tmp.Day] = c;
    60         c++;
    61         tmp.nextDay();
    62     }
    63 }
    64 int main()
    65 {
    66     preProcess();
    67     int y1,y2,m1,m2,d1,d2;
    68     while ( scanf("%4d%2d%2d",&y1,&m1,&d1)!=EOF )
    69     {
    70         scanf("%4d%2d%2d",&y2,&m2,&d2);
    71         int ans = abs(buffer[y1][m1][d1]-buffer[y2][m2][d2]);
    72         printf("%d
    ",ans+1);
    73     }
    74     return 0;
    75 }

    今年的第几天

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #define isRein(x) ((x%100!=0)&&(x%4==0)) || (x%400 ==0)?1:0
     6 using namespace std;
     7 /*
     8     题目:今年的第几天
     9     思路:减去本年的元旦那一天
    10 
    11 */
    12 int dayOfMonth[13][2] =
    13 {
    14   0,0,// 便于直接从1月开始访问
    15   31,31,
    16   28,29,
    17   31,31,
    18   30,30,
    19   31,31,
    20   30,30,
    21   31,31,
    22   31,31,
    23   30,30,
    24   31,31,
    25   30,30,
    26   31,31
    27 };
    28 
    29 struct Date
    30 {
    31     int Year;
    32     int Month;
    33     int Day;
    34     void nextDay()
    35     {
    36         if (++Day > dayOfMonth[Month][isRein(Year)])
    37         {
    38             Day = 1;
    39             Month ++ ;
    40         }
    41         if (Month>12)
    42         {
    43             Month = 1;
    44             Year ++;
    45         }
    46 
    47 
    48     }
    49 };
    50 int buffer[5000][12][31];
    51 void preProcess()
    52 {
    53     Date tmp ;
    54     tmp.Day = 1;
    55     tmp.Month = 1;
    56     tmp.Year = 0;
    57     int c = 1;
    58     while (tmp.Year < 5000)
    59     {
    60         buffer[tmp.Year][tmp.Month][tmp.Day] = c;
    61         c++;
    62         tmp.nextDay();
    63     }
    64 }
    65 int main()
    66 {
    67     preProcess();
    68     int y1,d1,m1;
    69     while ( scanf("%4d %2d %2d",&y1,&m1,&d1)!=EOF )
    70     {
    71         int ans = abs(buffer[y1][m1][d1]-buffer[y1][1][1]);
    72         printf("%d
    ",ans+1);
    73     }
    74     return 0;
    75 }

    打印日期

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include <stdio.h>
     5 #define isRein(x) ((x%100!=0)&&(x%4==0)) || (x%400 ==0)?1:0
     6 using namespace std;
     7 /*
     8     题目:日期差值
     9 
    10 */
    11 int dayOfMonth[13][2] =
    12 {
    13   0,0,// 便于直接从1月开始访问
    14   31,31,
    15   28,29,
    16   31,31,
    17   30,30,
    18   31,31,
    19   30,30,
    20   31,31,
    21   31,31,
    22   30,30,
    23   31,31,
    24   30,30,
    25   31,31
    26 };
    27 
    28 struct Date
    29 {
    30     int Year;
    31     int Month;
    32     int Day;
    33     void nextDay()
    34     {
    35         if (++Day > dayOfMonth[Month][isRein(Year)])
    36         {
    37             Day = 1;
    38             Month ++ ;
    39         }
    40         if (Month>12)
    41         {
    42             Month = 1;
    43             Year ++;
    44         }
    45 
    46 
    47     }
    48 };
    49 int buffer[5000][12][31];
    50 void preProcess()
    51 {
    52     Date tmp ;
    53     tmp.Day = 1;
    54     tmp.Month = 1;
    55     tmp.Year = 0;
    56     int c = 1;
    57     while (tmp.Year < 5000)
    58     {
    59         buffer[tmp.Year][tmp.Month][tmp.Day] = c;
    60         c++;
    61         tmp.nextDay();
    62     }
    63 }
    64 int main()
    65 {
    66     preProcess();
    67     int y1,num;
    68     while ( scanf("%4d %d",&y1,&num)!=EOF )
    69     {
    70         int yuandan = buffer[y1][1][1];
    71         int today = yuandan + num -1 ;
    72         for (int i = 1 ; i < 13 ;i++)
    73             for (int j = 1;j < 32 ; j++)
    74         {
    75             if (today == buffer[y1][i][j])
    76                 printf("%d-%02d-%02d
    ",y1,i,j); // 控制输出格式,填充0
    77         }
    78 
    79 
    80     }
    81     return 0;
    82 }

    Day ofWeek

      1 #include <iostream>
      2 #include <algorithm>
      3 #include <string.h>
      4 #include <stdio.h>
      5 #define isLeap(x) (x%100 != 0 && x%4 == 0)||(x % 400 == 0)?1:0
      6 using namespace std;
      7 /*
      8     题目:Day ofweek
      9     用时:
     10     思路:1. 月份和星期日的转换器:利用字符串数组的索引对应字符串
     11         2. 已知:今天是星期四 2018 3/8
     12         3. 求:今天和求取的那天差了几天,以7位倍数循环
     13         4. 这两天谁在前,谁在后,如何通过7来求星期几
     14 
     15 */
     16 
     17     char tm[13][10] = {
     18     "",
     19     "January",
     20     "February",
     21     "March",
     22     "April",
     23     "May",
     24     "June",
     25     "July",
     26     "August",
     27     "September",
     28     "October",
     29     "November",
     30     "December"
     31     };
     32 
     33     char tw[7][10] = {
     34     "Sunday", // 0 注意这里Sunday是0
     35     "Monday", // 1
     36     "Tuesday",
     37     "Wednesday",
     38     "Thursday",
     39     "Friday",
     40     "Saturday"
     41     };
     42 
     43 int dayOfMonth[13][2]
     44 {
     45     0,0,
     46     31,31,
     47     28,29,
     48     31,31,
     49     30,30,
     50     31,31,
     51     30,30,
     52     31,31,
     53     31,31,
     54     30,30,
     55     31,31,
     56     30,30,
     57     31,31
     58 };
     59 struct Date
     60 {
     61     int year;
     62     int month;
     63     int day;
     64     void nextDay()
     65     {
     66         if (++day > dayOfMonth[month][isLeap(year)])
     67         {
     68             day = 1;
     69             month++;
     70         }
     71         if (month > 12 )
     72         {
     73             month = 1;
     74             year ++;
     75         }
     76     }
     77 };
     78 
     79 int buffer[3001][13][31];
     80 void preProcessing()
     81 {
     82     int c = 1;
     83     Date date;
     84     date.year = 0 ;
     85     date.month = 1;
     86     date.day = 1;
     87     while ( date.year < 3001)
     88     {
     89         buffer[date.year][date.month][date.day] = c;
     90         c++;
     91         date.nextDay();
     92     }
     93 }
     94 int numberMonth (char month[])
     95 {
     96     for (int i=1 ;i<13;i++)
     97     {
     98         if (strcmp(tm[i],month)==0)
     99         {
    100             return i;
    101         }
    102     }
    103     return 0;
    104 }
    105 
    106 int main()
    107 {
    108     preProcessing();
    109     int day,year;
    110     char Month[10];
    111     int today = buffer[2012][7][16];
    112     int month,thatDay;
    113     while (scanf("%d %s %d",&day,Month,&year)!=EOF)
    114     {
    115         month = numberMonth(Month);
    116         thatDay = buffer[year][month][day];
    117         // 如何根据差值计算星期几thatDayw?
    118         int days = thatDay - today +1;
    119         printf("%s
    ",tw[(days%7+7)%7]);
    120 
    121 
    122 
    123     }
    124     return 0;
    125 }
  • 相关阅读:
    HTML style基础2
    HTML 笔记 基础1
    qq发送邮件
    Requests+Excel接口自动化测试(Python)
    echars前端处理数据、pyechars后端处理数据
    appium基础一:连接手机和appium-desktop定位元素
    Loadrunner 性能测试工具笔记
    总结windows cmd 查看进程,端口,硬盘信息
    appium + python 自动化调试手机时 UiAutomator exited unexpectedly with code 0, signal null
    什么是接口测试?怎样做接口测试?
  • 原文地址:https://www.cnblogs.com/twomeng/p/9509425.html
Copyright © 2020-2023  润新知