C语言解决“三天打鱼两天晒网”的问题?

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

    根据题意可以将解题过程分为三步:
    (1)计算从1990年1月1日开始至指定日期共有多少天?
    (2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除。
    (3)根据余数判断他是“打鱼”还是“晒网”。
        若余数为1,2,3,则他是在“打鱼”,否则他是在“晒网”。

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

#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);
}

程序运行结果:

猜你喜欢

转载自blog.csdn.net/xingyuncao520025/article/details/131258142
今日推荐