计算13日星期五的未来发生次数 [英] Calculating future occurrences of Friday the 13th

查看:86
本文介绍了计算13日星期五的未来发生次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望可以从一年开始,并计算13日星期五的发生次数.暴力解决方案很简单而且很明显.我有一些更好的东西,但是毫无疑问,其他人可以为此提出一个优雅的算法.

I'd like to be able to start with a year, and calculate occurrences of Friday the 13th. A brute force solution is easy and obvious. I have something slightly better, but I have no doubt that someone else can come up with an elegant algorithm for this.

也许有点棘手,我想给这个程序一个月的时间,让它找到第二年的那个月,该月的13号是星期五.

Perhaps a little trickier, I'd be interested in giving the program a month, and have it find the next year in which that month has a Friday the 13th.

可以随意使用伪代码,但是我希望人们会为使用您喜欢的语言的有效代码示例投票更多.

Feel free to use pseudo code, but I expect people will vote more for working code samples in you favorite language.

推荐答案

从星期日开始的每个月的第13个星期五.只有14种组合可以知道一年的第一天(有或没有with年,以及周日).您只需要计算一次就可以了.您只需要检查14 * 12个月就可以开始使用,当然这是有道理的.

Any month that starts with a Sunday has a Friday on the thirteenth. There are only 14 combinations possible knowing what day the first of the year is on (with or without leap year, and sun-sat). You should just calculate it once and get it over with. You'd only check 14*12 possible months to start out with, well with in reason.

结果表格元素(从2009年,2010年开始):

resultant table element (from 2009, 2010):

[Thursday,false] => Feb, March, Nov
[Friday,false] => Aug

要填写表格,您有一个普通的月份Jan(31),Feb(28)..然后用一周中每一天的种子进行迭代,注意从星期日开始的月份,以及with年和没有.非常简单,一旦完成,您就可以与我们分享:)

to fill the table you have a generic month Jan(31),Feb(28).. and then iterate with a seed of each day of the week, noting months that start with sunday, and also with a leap year and without. Pretty straight forward, and once done, you can share it with us :)

这篇关于计算13日星期五的未来发生次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆