• 蓝桥杯 5-3日历 日期计算


    问题描述
      已知2007年1月1日为星期一。设计一函数按照下述格式打印2007年以后(含)某年某月的日历,2007年以前的拒绝打印。为完成此函数,设计必要的辅助函数也是必要的。
    样例输入
    一个满足题目要求的输入范例。
    例:
    2050 3
    样例输出
    与上面的样例输入对应的输出。
    例:

    数据规模和约定
      输入数据中每一个数的范围。
      例:年 2007-3000,月:1-12。
    转载自https://blog.csdn.net/weixin_40124642/article/details/78442406
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 bool is_leap(int y) {
     4     if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { //四年一闰,百年不闰,四百年又闰的代码表示 
     5         return true;
     6     } else {
     7         return false;
     8     } 
     9 }
    10 int a[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    11 int main() {
    12     int y, m;
    13     cin >> y >> m;
    14     if (is_leap(y)) {
    15         a[2]++;
    16     }
    17     int c = 1; //今年到该月一号的天数. 
    18     for (int i = 0; i < m; i++) {
    19             c += a[i];
    20     }
    21     int s = (y - 1) + ((y - 1) / 4) + ((y - 1) / 400) - ((y - 1) / 100) + c;
    22     s %= 7;
    23     //y年m月1日的星期为s 
    24     //cout << "week = " << s << endl;
    25     cout << "Calendar " << y << " - ";
    26     if (m < 10) {
    27         cout << 0;
    28     } 
    29     cout << m << endl;
    30     cout << "---------------------" << endl;
    31     cout << "Su Mo Tu We Th Fr Sa " << endl;
    32     cout <<"---------------------"<<endl;
    33     for (int i = 0; i < s; i++) {
    34         cout << "   ";
    35     }
    36     for (int i = 1; i <= a[m]; i++) {
    37         if ((s + i - 1) % 7 == 0) {
    38             cout << endl;
    39         } 
    40         if (i < 10) {
    41             cout << " ";
    42         }
    43         cout << i << " ";
    44     }
    45     cout << endl;
    46     cout << "---------------------" << endl;
    47     return 0;
    48 }
    49 /*
    50 计算公元某年某月某日是星期几,是这样的一个算式:
    51 S=(y-1)+[(y-1)/4]-[(y-1)/100]+[(y-1)/400]+C 
    52 其中y是公元的年数,C是从这一年的元旦算起到这一天为止(包括这一天是内)的天数.
    53 方括号表示其中算式的整数部分,即在计算S的值时,三个方括号中只要算出商数的整数部分,把余数略去不计.
    54 求出S的值之后,除以7,余几就是星期几;除尽了就是星期日.
    55 我们来计算一下2008年12月25日是星期几?此时x=2008 C=360于是可得 
    56 :S=(2008-1)+[(2008-1)÷4]-[(2008-1)÷100]+[(2008-1)÷400]+360=2853 3853÷7=407……4 
    57 那么2008年的12月25日是星期四
    58 */
  • 相关阅读:
    HDU 2899 Strange fuction
    HDU 2899 Strange fuction
    HDU 2199 Can you solve this equation?
    HDU 2199 Can you solve this equation?
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 699 掉落的方块(线段树?)
    Java实现 LeetCode 699 掉落的方块(线段树?)
    Java实现 LeetCode 699 掉落的方块(线段树?)
  • 原文地址:https://www.cnblogs.com/fx1998/p/12720188.html
Copyright © 2020-2023  润新知