一 .问题描述
中国有句古话叫三天打鱼,两天晒网,默认从1990年1月1日开始"三天打鱼,两天晒网",问这个人在以后的某一天打鱼还是晒网?
二.问题分析
1>计算从1990年1月1日开始到指定日期共有多少天
2>由于打鱼晒网周期为5天,故将计算的天数用5整除
3>跟给余数判断为打鱼还是晒网,打鱼的余数为1,2,3
三.算法设计
采用数值计算算法,利用循环求出 ,
1990年1月1日开始到指定日期共有多少天,考虑闰年2月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, const char * argv[])
{
// insert code here...
DATE today;
int totalDay;
int result;
printf("请输入日期,包括年月日如 1999 1 30");
scanf("%d%d%d",&today.year,&today.month,&today.day);
totalDay=countDay(today);
result=totalDay%5;
if (result>0&&result<4)
{
printf("今天打鱼");
}
else
printf("今天晒网");
return 0;
}
//判读某一年是否为闰年
int runYear(int year)
{
if ((year%4==0&&year%100!=0)||(year%4==0))
{
return 1;
}
else
return 0;
}
//计算制定日期距离1990年一月一号的天数
int countDay(DATE cunrrentDay)
{
int perMonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int totolaDay=0,year,i;
for (year=1990; year<cunrrentDay.year; year++)
{
if (runYear(year)) {
totolaDay+=366;
}
else
totolaDay+=365;
}
//如果是闰年二月份为29天
if (runYear(cunrrentDay.year))
{
perMonth[2]+=1;
}
//吧本年内的天数累加到totalDay中
for (i=0; i<cunrrentDay.month; i++)
{
totolaDay+=perMonth[i];
}
//把本月的天数累加到totalDay中;
totolaDay+=cunrrentDay.day;
return totolaDay;
}