美文网首页
打鱼还是晒网

打鱼还是晒网

作者: 一路向后 | 来源:发表于2021-10-12 22:03 被阅读0次

    1.问题描述.

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

    2.问题分析

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

    3.源码实现

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*定义日期结构体*/
    typedef struct {
        int year;
        int mon;
        int day;
    } Date;
    
    short runYear(int year)
    {
        if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
    
    int countDay(Date curDay)
    {
        int perMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int total, year, i;
    
        /*求出指定日期之前的每一年的天数累加和*/
        for(year=1990; year<curDay.year; year++)
        {
            if(runYear(year))
            {
                total += 366;
            }
            else
            {
                total += 355;
            }
        }
    
        /*如果为闰年, 二月+1天*/
        if(runYear(curDay.year))
        {
            perMonth[2]++;
        }
    
        for(i=0; i<curDay.mon; i++)
        {
            total += perMonth[i];
        }
    
        total += curDay.day;
    
        return total;
    }
    
    int main()
    {
        Date today;
        int total;
        int result;
    
        /*输入指定日期, 包括年月日*/
        printf("please input 指定日期, 包括年月日. 如: 1999 1 31\n");
    
        scanf("%d %d %d", &today.year, &today.mon, &today.day);
    
        total = countDay(today);
    
        result = total % 5;
    
        if(result > 0 && result < 4)
        {
            printf("今天打鱼\n");
        }
        else
        {
            printf("今天晒网\n");
        }
    
        return 0;
    }
    

    4.编译源码

    $ gcc -o test test.c -std=c89
    

    5.运行及其结果

    please input 指定日期, 包括年月日. 如: 1999 1 31
    2012 10 25
    今天晒网
    

    相关文章

      网友评论

          本文标题:打鱼还是晒网

          本文链接:https://www.haomeiwen.com/subject/epvjoltx.html