寻找第n个工作日的算法

标签 algorithm

<分区>

我已经设计了一个程序来查找第 n 个工作日而不使用循环。

请就此提出您的建议-

操纵工作日的算法-

问题:找出从任意一天开始的第 n 个工作日的日期。

解决方法:

  1. 归一化到最近的星期一 -

    If today(or the initial day) happens to be something other than monday, bring the day to the closest monday by simple addition or subtraction.
    

    例如:初始日 - 10 月 17 日。这恰好是星期三。因此,通过减少 2 个日期来将这个 no monday 正常化。 现在将这 2 个日期命名为初始标准化因子。

  2. 添加这些周中的工作日数 + 周末数。

    例如:要增加 10 个工作日,我们需要增加 12 天。由于 10 天有 1 周,其中仅包括 1 个星期六和 1 个星期日。 这是因为,我们正在规范化到最近的星期一。

  3. 摊销返还-

    现在从结束日期开始添加初始归一化因子(对于负初始归一化)和另一个常数因子(例如,k)。 或者如果初始归一化是从星期五获得的,则加 1,这恰好是 +3。 如果开始日期在 Saturday 和 Sunday ,则视为星期一。所以这一步不需要摊销。

    例如:假设初始归一化从星期三开始,则初始归一化因子为 -2。因此,将 2 添加到结束日期和常数 k。

    The constant k is either 2 or 0. 
    

常量定义-

    If initial normalization factor is -3, then add 2 to the resulting date if the day before amortization is (wed,thu,fri) 
    If initial normalization factor is -2, then add 2 to the resulting date if the day before amortization is (thu,fri) 
    If initial normalization factor is -1, then add 2 to the resulting date if the day before amortization is (fri) 

例子-

   Find the 15th working day from Oct,17 (wednesday).

第 1 步 -

初始归一化 = -2 现在开始日期是 10 月 15 日(星期一)。

第 2 步 -

add 15 working days -

15 days => 2 weeks
    weekends = 2 (2 sat, 2 sun)

    so add 15 + 4 = 19 days to Oct, 15 monday.

    end_date = 2, nov, Friday

步骤 3a -

end_date = end_date + initial normalization = 4, nov sunday

步骤 3b -

end_date = end_date + constant_factor = 4, nov, sunday + 2 = 6, nov (Tuesday)

交叉验证-

 Add 15th working day to Oct, 17 wednesday

 Oct,17 + 3 (Oct 17,18,19) + 5 (Oct 22-26) + 5 (Oct 29 - Nov 2)  + 2 (Nov 5, Nov 6)

 Now the answer is 6, Nov, Tuesday.

我用几个案例验证过。请分享您的建议。

拉森。

最佳答案

首先,它是一个很好的算法,但我对边界条件有疑问:例如,如果我需要从今天开始找到第 0 个工作日怎么办:

第 1 步 -

initial normalization = -2 now start date is Oct,15 (monday).

第 2 步 -

add 0 working days -

0 days => 0 weeks
    weekends = 0
    so add 0 + 0 = 0 days to Oct, 15 monday.

    end_date = 15, oct, monday

步骤 3a -

end_date = end_date + initial normalization = 17, oct wednesday

步骤 3b -

end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.

现在让我们重复查找从今天开始的第一个工作日的步骤:

第 1 步 -

initial normalization = -2 now start date is Oct,15 (monday).

第 2 步 -

add 1 working days -

1 days => 0 weeks
    weekends = 0
    so add 1 + 0 = 1 days to Oct, 15 monday.

    end_date = 15, oct, monday

步骤 3a -

end_date = end_date + initial normalization = 17, oct wednesday

步骤 3b -

end_date = end_date + constant_factor = 17, Oct wednesday or 19,oct friday based on whether constant factor is 0 or 2 as it be only one of these values.

您是否注意到,算法为 0 和 1 给出了相同的最终结果。如果事先定义 0 个工作日和 1 个工作日被视为相同的场景,那么这可能不是问题,但理想情况下它们应该给出不同的结果.

我还建议您考虑负面测试用例,比如如果我需要从今天起找到第 -6 个工作日怎么办,您的算法会正确地给我过去的日期吗?

关于寻找第n个工作日的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12927337/

相关文章:

algorithm - 为什么 Apple FileVault 使用 block 加密算法而不是流加密算法?

从传感器值导出枚举值的算法?

algorithm - 除了 Haar 级联之外,还有哪些算法或方法可用于自定义对象检测?

algorithm - 高效线段-三角形相交

image - 匹配不同格式的两个图像

javascript - 从多个对象中找到最接近的 x 属性值之和

algorithm - 移除闭合网格上的遮挡面

java - 无法从静态上下文错误中引用非静态方法

arrays - 如何从数组中生成所有长度为偶数的子序列?

java - DCT实现