这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 有奖活动 » 有趣的黑色星期五代码

共2条 1/1 1 跳转至

有趣的黑色星期五代码

工程师
2025-08-09 08:25:26     打赏

#include <stdio.h>

#include <string.h>


// 判断是否为闰年

int isLeapYear(int year) {

    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}


// 计算指定年份黑色星期五数量并列出具体日期

void GetYearBlackFriday(int year) {

    int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    char *months[12] = {"一月", "二月", "三月", "四月", "五月", "六月", 

                        "七月", "八月", "九月", "十月", "十一月", "十二月"};

    

    if (isLeapYear(year)) {

        days[1] = 29;

    }

    

    int count = 0;

    int totalDays = 0;

    

    // 计算从1900年到指定年份前一年的总天数

    for (int y = 1900; y < year; y++) {

        if (isLeapYear(y)) {

            totalDays += 366;

        } else {

            totalDays += 365;

        }

    }

    

    printf("%d年的黑色星期五日期如下:\n", year);

    

    for (int month = 0; month < 12; month++) {

        // 计算当前月份13号是星期几

        int weekday = (totalDays + 13) % 7;

        

        if (weekday == 5) {

            printf("%s 13日\n", months[month]);

            count++;

        }

        

        // 累加当前月份的天数

        totalDays += days[month];

    }

    

    printf("\n%d年共有%d个黑色星期五\n", year, count);

}


int main() {

    int year;

    while (1) {

        printf("请输入年份(输入0退出):");

        scanf("%d", &year);

        

        if (year == 0) {

            break;

        }

        

        if (year < 1900) {

            printf("年份必须大于等于1900\n");

            continue;

        }

        

        GetYearBlackFriday(year);

    }

    

    return 0;

}



专家
2025-08-09 13:38:47     打赏
2楼

感谢分享


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]