中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。

问题描述:中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。

/*
 *  作者:cloud9
 *  日期:2019.2.27
 *
 *  问题描述:中国有句俗语叫“三天打鱼两天晒网”。某人从2010年1月1日起开始“三天打鱼两天晒网”,
 *  问这个人在以后的某一天中是“打鱼”还是“晒网”。
 *
 *  
 *
 *
*/


我的思路是先算从2010.1.1日到给定年限的前一年共有多少天,再加上给定年的天数,得到总天数,再用总天数
对5求模,如果余1,2,3则说明在打渔否则说明在晒网。但是这么算有一个问题——在大于2010年后会多算一天。
原因在于我们会将给定日期的那一年的1月1日算两次。因为从2010.1.1到2011.1.1才有365天,但是我在单独
计算2011年到某月某日有多少天时又把2011.1.1这一天计算了一次。




#include<iostream>
#include<fstream>
#include<cstdlib>
const int MON_N = 13;
using namespace std;


int a[MON_N] = {0,31,28,31,30,31,30,31,31,30,31,30,31};

ofstream out;
ifstream infile;

int main(int argc, char const *argv[])
{	
	int Judge(const int & year1,const int & month1,const int & day1);		//函数原型声明
	int year,month,day;
	infile.open("in.txt");
	if(infile.is_open())
	{
		cout << "infile打开文件成功" << endl;
	}
	out.open("out.txt");
	if (out.is_open())
	{
		cout << "out打开文件成功" << endl;
	}
	while (!infile.eof())
	{	
		infile >> year >> month >> day;
		if (year < 2010 || (month > 12 && month <= 0) || (day > 31 && day < 28))	//判断输入数据是否符合正常年月日的范围
		{
			out << "Error" << endl;
			break;
		}
		int flag = 0;
		flag = Judge(year, month, day);
		if (flag == 0)
		{
			out << "Fishing" << endl;
			cout << "Fishing" << endl;
		}
		else if (flag == 1)
		{
			out << "Sun net" << endl;
			cout << "Sun net" << endl;
		}
		else
			break;
	}
		infile.close();
		out.close();
		system("pause");
	return 0;
}



int	Judge(const int & year1,const int & month1,const int & day1)		//判断从2010.1.1到指定日期共经过了多少天,再求5的模根据余数判断是在打渔还是晒网
{	
	int sum_year = 0;
	for (int i = 2010; i < year1; i++)		/*计算从2010年到指定年限的前一年有多少天*/
	{
		if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))		/*判断某一年是否是闰年,是闰年则加上366,不是则加上365*/												 
		{
			sum_year += 366;
		}
		else
			sum_year += 365;
	}
	if(!(year1 % 400 == 0 || (year1 % 100 != 0 && year1 % 4 == 0 )))
	{	
		if (month1 == 2 && day1 == 29)    /*判断当不是闰年出现2月29的情况下重新输入*/
		{
			out << "Error" << endl;
			return 2;
		}
	}
	else
	{
		if(month1 > 2)
			sum_year++;
	}
	for (int j = 0;j < month1 ;j++)
		{
			sum_year += a[j];
		}
	sum_year += day1;
	if (year1 > 2010)		/*因为在计算总天数时将输入年份的1月1日这一天计算了两次,因此要减去一天*/
		sum_year--;
	if (sum_year % 5 <= 3 && sum_year % 5 >= 1)		 /*当余数是1,2,3时,说明在打渔,否则在晒网*/
		return 0;
	else
		return 1;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值