首页 > 其他 > 详细

初级系列3.打鱼还是晒网问题

时间:2019-09-08 10:03:34      阅读:83      评论:0      收藏:0      [点我收藏+]

打鱼还是晒网

问题描述
某人从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>

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

int countDay(DATE);
int runYear(int);

int main(int argc, char **argv)
{
    DATE today;
    int totalDay;
    int result; //对5取余的结果
    printf("please input 指定日期 包括年, 月, 日, 如: 1999 1 31\n");
    scanf("%d%d%d", &today.year, &today.month, &today.day); //输入指定日期, 包括年, 月,日
    totalDay = countDay(today); //求出指定日期距离1990年1月1日的天数
    result = totalDay % 5; //天数%5 判断输出打鱼还是晒网
    if(result > 0 && result < 4)
        printf("今天打鱼");
    else
        printf("今天晒网");

    return 0;
}

/*判断是否为闰年,是返回1,否返回0*/
int runYear(int year)
{
    if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        return 1;
    else
        return 0;
}

/*计算指定日期距离1990年1月1日的天数*/
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))    //如果为闰年, 则二月份为29天
    {
        perMonth[2] += 1;
    }
    for(i = 0; i < currentDay.month; i++) //将本年内的天数累加到totalDay中
        totalDay += perMonth[i];
    totalDay += currentDay.day; //将本月内的天数累加到totalDay中
    return totalDay;
}
c_year = int(input("please input year"))
c_month = int(input("please input month"))
c_day = int(input("please input day"))

def runYear(year):
    if ((year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)):
        return 1
    else:
        return 0

def countDay(c_year, c_month, c_day):
    totalDay = 0
    month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    for year in range(1990, c_year):
        if runYear(year):
            totalDay = totalDay + 366
        else:
            totalDay = totalDay + 365
    if (runYear(c_year)):
        month[2] += 1
    for i in range(c_month):
        totalDay += month[i]
    totalDay += c_day
    return totalDay

totalDay = countDay(c_year, c_month, c_day)
result=totalDay % 5
if result > 0 and result < 4:
    print("today fishing")
else:
    print("today internet")

初级系列3.打鱼还是晒网问题

原文:https://www.cnblogs.com/xuzhaoping/p/11484395.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!