2023年5月4日记录

求出指定日期距离1990年1月1日的天数
这里为整个算法的核心部分。经过分析可以得到指定日期距离 1990年1月1日的天数totalDay=1990年至指定年的前一年共有多少天+指定年中到指定日期的天数。由于每月天数不同,可以设置一个月份数组 int perMonth[13],存放每月的天数。程序利用年份作为循环变量,要判断指定年份之前的每一年是否为闰年,若为闰年则执行 totalDay=totalDay+366,否则执行 totalDay-totalDay+365;对于指定年份,也要判定是否为闰年,然后根据月份数,将每月的天数累加到 totalDay 中。perMonth 数组的初始化设置如表 1.1 所示。perMonth 数组设置含有 13 个元素,perMonth[0]元素并不使用。原因在于这种设置可以使数组下标和月份对应,便于编程设置循环变量,数组中2月天数初始设置为 28,如果当前年份为闰年,则需要执行 perMonth[2]++操作。

 

 

 

#include<stdio.h>
typedef struct date{

int year;
int month;
int day;
}DATE;
int countDay(DATE);
int runYear(int);
void main()
{
DATE today;
int totalDay;
int result;

printf("pleaseinput 指定日期 包括年,月,如:1999131n");
scanf("d%dgd"&today.year,&today.month,&today.day);
totalDay=countDay(today);
result=totalDay%5;

if(result>0&&result<4)
printf("今天打鱼");
else
printf("今天晒网");
}
int runYear(int year)
{
if((year%4==0&&year%100!=0)11(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.301;
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[il;
totalDay+=currentDay.day;
return totalDay;
}

posted @ 2023-05-04 23:21  suN(小硕)  阅读(19)  评论(0编辑  收藏  举报