在西方,星期五和数字 13 都代表着坏运气,两个不幸的个体最后结合成超级不幸的一天。所以,不管哪个月的13日又恰逢星期五就叫 “ 黑色星期五 ” 。输入某年年号和该年的元旦是星期几( 1-7),输出该年所有的 “ 黑色星期五 ” 的日期(年 /月/日) 。用c语言
时间: 2024-11-04 15:16:46 浏览: 85
要在C语言中编写一个程序来找出特定年份中的所有黑色星期五,你需要遍历每个月的13日,并检查这一天是否是星期五。这里是一个简单的示例代码,使用`mktime()`函数来计算日期对应的星期数:
```c
#include <stdio.h>
#include <time.h>
int is_leap_year(int year) {
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int day_of_week(int year, int month, int day) {
struct tm date = {0};
date.tm_year = year - 1; // January = 0
date.tm_mday = day;
if (is_leap_year(year)) {
date.tm_yday += 1; // Leap years have an extra day in February
}
time_t t = mktime(&date);
return t % 7 + 1; // 0-6 represents Sunday-Saturday
}
// Function to find black Friday dates
void find_black_fridays(int year, int start_day) {
for (int month = 1; month <= 12; ++month) {
for (int day = 13; day <= 31; ++day) {
int dow = day_of_week(year, month, day);
if (dow == start_day && dow == 5) { // Friday and the specified starting day
printf("%d/%02d/%02d\n", year, month, day);
}
}
}
}
int main() {
int user_year;
printf("Enter a year: ");
scanf("%d", &user_year);
// Ensure it's not a leap year if the first of the year is not Monday
if (day_of_week(user_year, 1, 1) != 1) {
user_year--;
}
int start_day = day_of_week(user_year, 1, 1); // Calculate the starting day of the week
find_black_fridays(user_year, start_day);
return 0;
}
```
这个程序首先会检查给定年份是否应该调整,如果年初不是星期一,则往前推一年,因为黑色星期五总是出现在星期五而非星期一。然后,它会找出所有月份的13日,判断这些日期是不是星期五。如果是星期五,并且与指定的开始周日一致(假设用户输入了星期几作为start_day),则输出该日期。
请注意,这只是一个基础实现,实际应用中可能还需要进行错误检查和其他增强功能。
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![ppt](https://img-home.csdnimg.cn/images/20241231044937.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)