1.3算法入门之打渔晒网

.问题描述

中国有句古话叫三天打鱼,两天晒网,默认从199011日开始"三天打鱼,两天晒网",问这个人在以后的某一天打鱼还是晒网?

.问题分析

1>计算从199011日开始到指定日期共有多少天

2>由于打鱼晒网周期为5,故将计算的天数用5整除

3>跟给余数判断为打鱼还是晒网,打鱼的余数为1,2,3

.算法设计

采用数值计算算法,利用循环求出 ,

199011日开始到指定日期共有多少天,考虑闰年229,平年为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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值