查找一段时间内每个月的第 13 号是哪一天

发布于 2024-10-01 05:58:27 字数 469 浏览 1 评论 0原文

我目前正在尝试解决 USACO 培训网站上的一些问题,为一场不相关的​​ C++ 编程比赛做准备。

但是,我陷入了这个问题:

每月 13 号出现在星期五的频率是否比一周中其他任何一天都少?为了回答这个问题,请编写一个程序,计算给定的 N 年时间内每月 13 日出现在星期日、星期一、星期二、星期三、星期四、星期五和星期六的频率。测试的时间段为1900年1月1日至1900年12月31日+给定年数N-1,N。N为非负数,不会超过400。

数字 N 在输入文件中提供输出是一个包含七个数字的文件,每个数字代表一周中某一天的 13 号数字。

我想知道你们会如何解决这个问题。我不是在寻找代码或任何东西,因为这只会违背我这样做的目的,相反,只是一个起点或一个算法会有所帮助。

到目前为止,我唯一能想到的就是使用世界末日算法,但是我不确定如何在代码中实现它。

任何帮助将不胜感激。

I am currently trying to solve some problems from the USACO training website in preparation for an unrelated C++ programming competition.

However, I am stuck on this problem:

Does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is non-negative and will not exceed 400.

The number N is provided in an input file and the output is to be a file with seven numbers in it, each representing the number of 13th's falling on a particular day of the week.

I was wondering how you guys would approach this problem. I am not looking for code or anything since that would just defeat the purpose of me doing this, instead just a starting point or an algorithm would be helpful.

So far the only thing I could think of is using the Doomsday Algorithm, however I am unsure about how I would implement that in code.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酷到爆炸 2024-10-08 05:58:27

正如 Denny 所说,N 非常小,您可以使用一个月中的天数表和一个简单的闰年谓词来轻松遍历月份来处理二月。只需找出 1900 年 1 月 13 日是哪一天,然后将 2 月 13 日、3 月 13 日等过去的天数相加。使用 % 运算符将已过去的天数换回星期几的值。

As Denny says, N is so small that you can easily iterate through the months using a table of days-in-a-month and a simple is-a-leap-year predicate to handle February. Just find out what day the 13th of Jan was in 1900 and then add up the elapsed days until 13th Feb, then 13th March etc.. Use a % operator to wrap the # of elapsed days back into a day-of-week value.

空袭的梦i 2024-10-08 05:58:27

N小于400?那么你最多只需要超过 365.25*400=146100 天。听起来很容易枚举所有这些,将日期转换为年/月/日期(使用您最喜欢的日期转换例程),测试星期几是微不足道的。

不过我会预先计算一下表格。

N is less than 400? well you just need to go over 365.25*400=146100 days at max. sounds easy to enumerate all of them, convert dates into year/month/date (with your favorite date conversion routine), testing for day of week is trivial.

I would precalculate the table though.

醉生梦死 2024-10-08 05:58:27

就用蛮力吧。就像这个伪代码示例:

from datetime import date

day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
             'Saturday', 'Sunday']
counts = [0] * 7
for year in range(1900, 2300):
    for month in range(1, 13):
        counts[date(year, month, 13).weekday()] += 1
for day, count in zip(day_names, counts):
    print('%s: %d' % (day, count))

“困难”部分是计算日期所在的星期几。在 C(++) 中,您可以使用 mktimelocaltime 库函数如果您知道您的平台可以处理大量数据足够的日期范围。

Just use brute force. Like this pseudocode example:

from datetime import date

day_names = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
             'Saturday', 'Sunday']
counts = [0] * 7
for year in range(1900, 2300):
    for month in range(1, 13):
        counts[date(year, month, 13).weekday()] += 1
for day, count in zip(day_names, counts):
    print('%s: %d' % (day, count))

The "hard" part is calculating the day of the week a date falls on. In C(++), you can use the mktime and localtime library functions if you know that your platform handles a large enough date range.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文