中国有句俗语叫“三天打鱼两天晒网”(C语言实现)

打鱼还是晒网问题(C语言实现)

1. 问题描述:

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

2. 算法设计考虑因素:

  1. 利用循环求指定日期到1990年1月1日的天数,考虑其中每年是否为闰年情况。闰年二月29天,平年28天。

  2. 判断闰年的条件:

 if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    能被4并且能被100整除的数或者能被400整除!否则为平年。

3. 代码展示块:

#include <stdio.h>
typedef struct date
{
    int year;
    int month;
    int day;
} DATE;
int countDay(DATE); //函数声明,计算在此期间共有的天数,在该函数中完成
int runYear(int); //判断是否为闰年,在该函数完成
int main()
{
    DATE today; //将上面结构体函数名改为today
    int totalday; //总天数
    int result;  //返回值处理totalday%5结果
    printf("请输入指定的日期(如:2001 2 34):"); //
    scanf("%d%d%d", &today.year, &today.month, &today.day);
    totalday = countDay(today);
    result = totalday % 5; //totalday % 5取余数,判断余数是否等于1,2,3在则输出打鱼,否则为晒网
    if (result > 0 && result < 4)  //判断余数
        printf("%d年%d月%d日出海打鱼", today.year, today.month, today.day);
    else
        printf("%d年%d月%d日在家晒网", today.year, today.month, today.day);
        return 0;
}
int runYear(int year)
{   // 能被4并且能被100整除的数或者能被400整除!否则为平年。
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))   
        return 1;
    else
        return 0;
}
int countDay(DATE currentDay)
{
    int perMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  //一年中每个月的天数
    int totalDay = 0, year, i;
    for (year = 1990; year <= currentDay.year; year++)
    {
        if (runYear(year))
            totalDay = totalDay + 366;
        else
            totalDay = totalDay + 365;
    }
    if (runYear(currentDay.year))  //判断输入的这一年是否为闰年,闰年2月则改变数组中的值+1天,变成29天 
        perMonth[2] += 1;
    for (i = 0; i < currentDay.month; i++)
        totalDay += perMonth[i];
    totalDay += currentDay.day;
    return totalDay;
}

4. 结果输出打印效果:
分别输入1990年1月1日之后任意连续几天的测试结果

  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值