C++趣味算法——3打鱼还是晒网
Posted 大头乐乐学编程
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++趣味算法——3打鱼还是晒网相关的知识,希望对你有一定的参考价值。
typedef struct date
{
int year;
int month;
int day;
}DATE;
int runYear(int year)//判断是否为闰年,是返回1,否返回0
{
if((year%4==0&&year%100!=0)||(year%400==0))
return 1;
else
return 0;
}
int countDay(DATE currentDay)
{
int perMonth[13]={0,21,28,31,30,31,30,31,30,30,31,30,31};//每月天数数组
int totalDay = 0,year = 0,i = 0;
for(year=2010;year<currentDay.year;year++)//到指定日期的前一年,每年天数累加
{
if(runYear(year))//判断闰年
totalDay += 366;
else
totalDay += 365;
}
if(runYear(currentDay.year))//闰年2月天数为29天
perMonth[2]+=1;
for(i=1;i<currentDay.month;i++)
totalDay+=perMonth[i];//将本年内天数累加到totalDay中
totalDay+=currentDay.day;//将本月内的天数累加到totalDay中
return totalDay;
}
using namespace std;
typedef struct date
{
int year;
int month;
int day;
}DATE;
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,21,28,31,30,31,30,31,30,30,31,30,31};
int totalDay = 0,year = 0,i = 0;
for(year=2000;year<currentDay.year;year++)
{
if(runYear(year))
totalDay += 366;
else
totalDay += 365;
}
if(runYear(currentDay.year))
perMonth[2]+=1;
for(i=0;i<currentDay.month;i++)
totalDay+=perMonth[i];
totalDay+=currentDay.day;
return totalDay;
}
int main(void)
{
DATE today;
int totalDay;
int result;
printf("输入年月日,例如:2010 1 1\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");
return 0;
}
以上是关于C++趣味算法——3打鱼还是晒网的主要内容,如果未能解决你的问题,请参考以下文章