Q:
A:
首先判断是否是小于20110101,如果小于则返回,然后调用number方法,首先判断是否是闰年,然后计算距离20110101多少天,然后跟5作余数,判断打渔还是晒网。
#include<iostream>
#include<string>
using namespace std;
//Is it a leap year?
int leap(int a) {
if ((a % 4 == 0 && a % 100 != 0) || a % 400 == 0) {
return 1;
} else {
return 0;
}
}
//judge date number
string number(int year, int month, int day){
//save leap
int b[12] = {31,29,31,30,31,30,31,31,30,31,30,31};
//save Ordinary
int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int days = 0;
int monthDay = 0;
string status = "";
if(leap(year) == 1){
for (int i = 0; i < month-1; ++i) {
monthDay+=b[i];
}
days = (year-2011)*366+monthDay+day;
}else{
for (int i = 0; i < month-1; ++i) {
monthDay+=a[i];
}
days = (year-2011)*365+monthDay+day;
}
days = days%5+1;
if(days > 3){
status = "晒网";
}else{
status = "打渔";
}
return status;
}
int main() {
string status = "";
string ymd = "";
cout<<"input date:"<<endl;
cin >> ymd;
if(20110101>atoi(ymd.c_str())){
cout<<"date less than 20110101"<<endl;
return 0;
};
string year = ymd.substr(0, 4);
string month = ymd.substr(4, 2);
string days = ymd.substr(6, 2);
status = number(atoi(year.c_str()),atoi(month.c_str()),atoi(days.c_str())-1);
cout<<status<<endl;
return 0;
}