相同类似问题:渔夫打鱼晒网问题
渔夫三天打鱼两天晒网,编程实现当输入2011年1月1日后的任意一天,输出该渔夫是在打鱼还是晒网
跟上题不同的是除了要计算该年多少天 加一个2011年1月1日以来过了几整年
//易错点:平年闰年没有写函数 调用两次 year不同 出错
//检验:输入2012 12 31 输出 732;输入2013 12 31 输出1096
代码:
//相同类似问题:渔夫打鱼晒网问题
//渔夫三天打鱼两天晒网,编程实现当输入2011年1月1日后的任意一天,输出该渔夫是在打鱼还是晒网
//跟上题不同的是除了要计算该年多少天 加一个2011年1月1日以来过了几整年
//易错点:平年闰年没有写函数 调用两次 year不同 出错
//检验:输入2012 12 31 输出 732;输入2013 12 31 输出1096
#include<bits/stdc++.h>
using namespace std;
int isok(int year){//因为要调用多次判断 单独写函数 以防出错
//判断平年闰年
int flag;
if((year%4==0&&year%100!=0)||(year%400==0)){
flag=1;//代表闰年
}else flag=0;//代表平年
return flag;
}
int main(){
int year,month,day;
cout<<"请输入2011年1月1日后的某年、月、日"<<endl;
cin>>year>>month>>day;
int a[12]={31,29,31,30,31,30,31,31,30,31,30,31};//闰年每月分别有多少天
int b[12]={31,28,31,30,31,30,31,31,30,31,30,31};//平年每月分别有多少天
int sum=0;//用来记录总天数
if(isok(year)==1){
for(int i=0;i<month-1;i++){//因为如果是12月的某一天,整数月有11个再加上12月的多少天
sum+=a[i];
}
} else{
for(int i=0;i<month-1;i++){
sum+=b[i];
}
}
sum=sum+day;
// cout<<sum<<endl;
for(int i=2011;i<year;i++){
if(isok(i)==1){
sum+=366;
}else{
sum+=365;
}
}
cout<<sum<<endl;
if((sum%5)>0&&(sum%5)<4){
cout<<"渔夫在打鱼"<<endl;
}else{
cout<<"渔夫在晒网"<<endl;
}
return 0;
}