算法【打漁曬網問題】

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章