C language solves the problem of "fishing for three days and drying the net for two days"?

There is a Chinese saying that goes, "Three days of fishing, two days of drying the net". Someone started "
    fishing Ask this person whether he will be "fishing" or "drying nets" on a certain day in the future?

    According to the meaning of the question, the problem-solving process can be divided into three steps:
    (1) Calculate the total number of days from January 1, 1990 to the specified date?
    (2) Since the cycle of "fishing" and "drying the net" is 5 days, the calculated number of days is divided by 5.
    (3) According to the remainder, judge whether he is "fishing" or "posting the net".
        If the remainder is 1, 2, 3, he is "fishing", otherwise he is "drying the net".

    Algorithm design:
            This algorithm is a numerical calculation algorithm. It is necessary to use a loop to find the number of days from the specified date to January 1, 1990
            , and take into account the leap year in the loop. February in leap years has 29 days, and
            February in normal years has 28 days.
            The way to judge a leap year is: if (divisible by 4 and not divisible by 100) or
            (divisible by 400), then the year is a leap year; otherwise it is not a leap year.

#include"stdio.h"

struct DATA   //创建年月日类型的数据
{
    int year;
    int month;
    int day;
};

int PanDuan(int year)
{
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
    {
        return 1;
    }
    else
        return 0;
}

int JiSuan(struct DATA d, int today)
{
    int year = 0;
    int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };  // 一年12个月,每个月的天数。
    ///  年的天数相加
    for (year = 1990; year < d.year; year++)  
    {
        if (PanDuan(year))   // 判断闰年还是平年,修改二月的天数
            arr[2] = 29;
        else
            arr[2] = 28;

        //把一年的天数加起来
        int j = 0;
        for (j = 0; j < 13; j++)
        {
            today += arr[j];
        }

    }

    //  把月的天数也加起来
    int month = 0;
    for (month = 0; month <= d.month-1; month++)
    {
        today += arr[month];
    }

    //  把天数加起来
    today += d.day;

    return today;
}

int main()
{
    struct DATA d;
    
    int today = 0;   // 距今天的总天数
    printf("请输入截止时间:");
    scanf("%d %d %d", &d.year, &d.month, &d.day);
    int day = JiSuan(d,today);
    if ((day % 5) > 0 && (day % 5) < 4)
        printf("距离今天共计:%d天,今天打鱼",day);
    else
        printf("距离今天共计:%d天,今天晒网",day);
}

Program running result:

 

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/131258142