三天打鱼两天晒网问题的C语言解决
中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。用C或C++语言/java/python实现程序解决问题。基本要求:1.程序风格良好(使用自定义注释模板),提供友好的输入输出。
提高要求:
1.输入数据的正确性验证。
2.使用文件进行数据测试。如将日期 20100101 20111214 等数据保存在in.txt文件中,程序读入in.dat文件进行判定,并将结果输出至out.txt文件。
由于本人水平有限,只能完成本题目的基本要求。。。。。。。
*问题分析与算法设计
根据题意可以将解题过程分为三步:1)计算从2010年1月1日开始至指定日期共有多少天;2)由于“打鱼”和“晒网”的周期为5天,所以将计算出的天数用5去除;3)根据余数判断他是在“打鱼”还是在“晒网”; 若 余数为1,2,3,则他是在“打鱼” 否则 是在“晒网” 在这三步中,关键是第一步。求从1990年1月1日至指定日期有多少天,要判断经历年份中是否有闰年,二月为29天,平年为28天。闰年的方法可以用伪语句描述如下:
如果 ((年能被4除尽 且 不能被100除尽)或 能被400除尽)
则 该年是闰年;
否则 不是闰年。
C语言中判断能否整除可以使用求余运算(即求模)
*源代码
#include<stdio.h>
int main()
{
int year,month,day,y,days=0;
int num[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
printf("Please enter the year:\n");
scanf("%d",&year);
printf("Please enter the month:\n");
scanf("%d",&month);
printf("Please enter the day:\n");
scanf("%d",&day);
while(year<2010) //*判断年份是否小于2010
{
printf("The value of the year you enterd was too low,please re-enterd:\n");
scanf("%d,%d,%d",&year,&month,&day);
}
while(month>12) //*判断年份是否大于12
{
printf("The value of the month you enterd was too high,please re-enterd:\n");
scanf("%d",&month);
}
for(y=2010;y<year;y++) //*判断年份是否为闰年,若为闰年则天数+366否则+365
{
if(y%400==0||(y%4==0 && y%100!=0))
days+=366;
else
days+=365;
}
if(year%400==0||(year%4==0 && year%100!=0)) //*当年份为闰年是,二月加一天
num[2]+=1;
for(y=1;y<month;y++) //*计算输入月份天数
{
days+=num[y];
}
days+=day;
printf("\n%d days passed from %d,%d,%d\n\n",days,day,month,year);
if(days%5==3||days%5==2||days%5==1) //*用计算出的总天数除以5,算出余数,根据余数判断
printf("The fisherman is fishing in %d,%d,%d.\n\n",day,month,year);
else
printf("The fsherman is drying the net in %d,%d,%d\n\n",day,month,year);
return 0;
}
*思路设计流程图
*程序运行效果图