三天打鱼两天晒网

问题:中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。

解答:该算法为数值计算算法,要利用循环求出指定日期距1990年1月1日的天数,并考虑到循环过程中的闰年情况,闰年二月为29天,平年二月为28天。判断闰年的方法可以用伪语句描述如下: 如果(能被4整除并且不能被100整除)或者(能被400整除)则该年是闰年;否则不是闰年。

image

#include <stdio.h>

struct date
{
    int year;
    int month;
    int day;
};

int days(struct date day);

int main(void)
{
    struct date today, term;
    int yearday, year, day;
    printf("Enter year/month/day");
    scanf("%d%d%d", &today.year, &today.month, &today.day);
    term.month = 12;
    term.day = 31;

    for (yearday = 0, year = 1990; year < today.year; year++)
    {
        term.year = year;
        yearday += days(term);
    }

    yearday += days(today);
    day = yearday % 5;

    if (day > 0 && day < 4)
        printf("he was fishing at that day/n");
    else
        printf("he was sleeping at that day./n");
}

int days(struct date day)
{
    static int day_tab[2][13] =
        {
            {
                0,
                31,
                28,
                31,
                30,
                31,
                30,
                31,
                31,
                30,
                31,
                30,
                31,
            },
            {
                0,
                31,
                29,
                31,
                30,
                31,
                30,
                31,
                31,
                30,
                31,
                30,
                31,
            },
        };

    int i, lp;
    lp = ((day.year % 4 == 0) && (day.year % 100 != 0)) || (day.year % 400 == 0);

    for (i = 1; i < day.month; ++i)
    {
        day.day += day_tab[lp][i];
    }

    return day.day;
}

image

posted @ 2023-04-20 09:38  笠大  阅读(132)  评论(0编辑  收藏  举报