算法【打渔晒网问题】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值