打魚還是曬網

中國有句俗話叫“三天打魚兩天曬網”。某人從1990年1月1日起開始“三天打魚兩天曬網”,問這個人在以後某一天中是打魚還是曬網。

根據題意可以講解題過程分爲3步:
(1)計算從1990年1月1日開始至指定日期共有多少天。
(2)由於“打魚”和“曬網”的週期爲5天,所以將計算出的天數用5去除
(3)根據餘數判斷他是在打魚還是曬網
若餘數是1,2,3,則他是在打魚,否則是在曬網

1/3/5/7/8/10/12都是31天
4/6/9/11都是30天
2月閏年爲29天,平年爲28天

#include<stdio.h>
//定義日期的結構體
typedef struct date{
    int year;
    int month;
    int day;
}DATE;
int countDay(DATE);
int runYear(int);
int main()
{
    DATE today;
    int totalDay;
    int result;
    printf("please input 指定日期 包括年,月,日 如:1999 1 31\n");
    scanf("%d%d%d",&today.year,&today.month,&today.day);
    totalDay=countDay(today);
    result=totalDay%5;
    if(result>0&&result<4)
        printf("今天打魚\n");
    else
        printf("今天曬網\n"); 
}
int runYear(int year)
{
    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))
        perMonth[2]+=1;
    for(i=0;i<currentDay.month;i++)
        totalDay+=perMonth[i];
    totalDay+=currentDay.day;
    return totalDay;
}
發佈了32 篇原創文章 · 獲贊 29 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章