【日期】黑色星期五
在西方,星期五和数字13都代表着坏运气,两个不幸的个体最后结合成超级不幸的一天。所以,不管哪个月的十三日又恰逢星期五就叫"黑色星期五”。
输入:
年份
输出:
判断该年是否包含黑色星期五,如包含,给出具体日期
样例:
序号 | 测试输入 | 期待的输出 | 额外进程 |
---|---|---|---|
1 | 2006↵ | There are 2 Black Fridays in year 2006.↵ They are:↵ 2006/1/13↵ 2006/10/13↵ | 0 |
思路
直接用我们之前写过的【日期】根据日期求星期
直接计算这一年内1-12月的十三号是否为星期五就可以了
代码
#include "stdio.h"
int a[13] = { 0,31,28,31,30,31,30,31,31,30,31 };
int OK(int y, int m)
{
int d = 13;
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
a[2] = 29;
int ans = d, i;
for (i = 1; i <= m - 1; i++)
ans += a[i];
int s, r;
s = y - 1 + (y - 1) / 4 - (y - 1) / 100 + (y - 1) / 400 + ans;
if (s % 7 == 5) return 1;
else return 0;
}
int main()
{
int y, count = 0, m[12];
scanf("%d", &y);
for (int i = 1; i < 13; i++)
if (OK(y, i)) m[count++] = i;
if (count > 1) printf("There are %d Black Fridays in year %d.\nThey are:\n", count, y);
else printf("There is %d Black Friday in year %d.\nIt is:\n", count, y);
for (int j = 0; j < count; j++)
{
printf("%d/%d/13\n", y, m[j]);
}
}
需要注意的是,只要是涉及英文表达的题目都要注意单复数,这都是血的教训QWQ
(我见一次错一次)