计算13号星期五的未来事件再度发生 [英] Calculating future occurences of Friday the 13th

查看:247
本文介绍了计算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.

随意使用伪$​​ C $ C,但我希望人们会投更多的工作code样品中,你最喜欢的语言。

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

推荐答案

任何一个月,与周日开始有一个周五的第十三位。目前只有14的组合可能知道何日今年第一是(带或不带闰年,和太阳-SAT)。你应该只计算一次,并结束它。你只检查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

填写表格,你有一个通用的月月(31),二月(28)..然后用迭代一周的每一天的种子,并指出月份开始的周日,也有一个闰年没有。 pretty的直线前进,一旦完成,你可以与我们分享:)

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天全站免登陆