【问题标题】:Finding t[] array in Sakamoto's algorithm to find the day of week在 Sakamoto 算法中查找 t[] 数组以查找星期几
【发布时间】:2014-07-19 21:11:43
【问题描述】:

我想知道如何在这个算法中找到如何计算t[] 数组的值?

int dow(int y, int m, int d)        
{        

   static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};        

   y -= m < 3;        

   return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;        

}

【问题讨论】:

  • 您可以选择一些您知道日期的随机日期。现在计算y -= m &lt; 3;(y + y/4 - y/100 + y/400 + d) % 7 的日期。现在将 t[m-1] 设置为一个值,以使您的答案正确。对所有 12 个 m 值执行此操作。检查这个公式的正确性read this post
  • 顺便说一下,这在 1752 年 9 月及之前将不起作用。
  • 我已经阅读了那篇文章,但我仍然无法找到 t[] 数组,请您举个例子来解释一下@MohitJain
  • 好的,我给你写一个小代码。

标签: c arrays algorithm dayofweek


【解决方案1】:

可以使用code below来生成需要的数组

#include <stdio.h>

int main(void) {
    int arr[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int totalDaysInThisYear = -1, m;  /* Parity would make totalDaysInThisYear = 0 */
    for(m = 1; m <= 12; ++m) {
        const int parity = (m < 3);
        printf("%d\n", (totalDaysInThisYear + parity) % 7);
        totalDaysInThisYear+= arr[m - 1];
    }
    return 0;
}

【讨论】:

  • 看起来 arr[] 不正确:元素太多,7 月和 8 月似乎在错误的位置。
  • @Bathsheba (+1) 谢谢你的指点,我会更正数组并使代码对 OP 更具可读性。
猜你喜欢
  • 2011-09-17
  • 1970-01-01
  • 2019-02-23
  • 2018-12-16
  • 2021-11-19
  • 2021-06-29
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多