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

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

问题描述

我想要能够从一年开始,并计算出13日的周五。蛮力解决方案很简单明了。我有点更好一点,但我毫不怀疑,其他人可以提出一个优雅的算法。



也许有点棘手,我会感兴趣给予该计划一个月,并找到明年的那个月有一个星期五13日。



随意使用伪代码,但我希望人们会对您最喜欢的语言的工作代码示例进行投票。



-Adam

解决方案

从星期天开始的任何一个月都有第十三个星期五。只有14个组合可能知道今年第一天(有或没有闰年,晒太阳)。你应该只是计算一次,并得到它。



生成的表格元素(从2009年的2010年):

  [Thursday,false] => Feb,March,Nov 
[Friday,false] => 8月

填写表,你有一个通用月份Jan(31),Feb(28)..然后用一周的每一天的种子进行迭代,注意到从星期天开始的几个月,并且还有一个闰年和没有。很简单,一旦完成,您可以与我们分享:)


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.

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.

-Adam

解决方案

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.

resultant table element (from 2009, 2010):

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

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