1.3打鱼还是晒网

1.问题描述
中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
2.问题分析
根据题意可以将解题过程分为3步:
(1)计算从1990年1月1日开始至指定日期共有多少天。
(2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除。
(3)根据余数判断他是在“打鱼”还是在“晒网”。
若余数为1,2,3,则他是在“打鱼”,否则是在“晒网”。
3.算法设计
该算法为数值计算算法,要利用循环求出指定日期距1990年1月1日的天数,并考虑到循环过程中的闰年情况,闰年二月为29天,平年二月为28天。

4.代码

#include<iostream>
using namespace std;

//判断输入的年份是否为闰年
bool is_leap_year(int year)
{
if ((year%400==0) || ((year%100!=0)&&(year%4==0)))
{
return true;
}
return false;
}

//计算输入的日期距离 1990 年 1 月 1 日一共多少天
int total_day(int year, int month, int day)
{
int total_num = 0;
int this_year = year;
int day_of_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};

while(this_year>1990)
{
if(!is_leap_year(this_year))
{
total_num += 365;
}
else
{
total_num += 366;
}
this_year--;
}

for(int i = 1; i<month; i++)
{
total_num += day_of_month[i-1];
}

//若日期大于二月并且该年是闰年,总天数 + 1
if(month>2 && is_leap_year(year))
{
total_num++;
}

total_num += day;
return total_num;
}

int main()
{
cout<<"请输入日期:"<<endl;
int year,month,day;
int total_number;

cin>>year;
cin>>month;
cin>>day;

total_number = total_day(year,month,day);
cout<<year<<"年"<<month<<"月"<<day<<"日距1990年1月1日一共有"<<total_number<<"天"<<endl;

int remainder = total_number % 5;

if(remainder==1 || remainder==2 || remainder==3)
{
cout<<"今天打鱼!"<<endl;
}
else
{
cout<<"今天晒网!"<<endl;
}

return 0;
}

posted @   kuku睡  阅读(31)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示