C语言题目之三天打鱼,两天晒网

题目内容:

中国有句俗语叫“三天打鱼两天晒网”,某人从1990年1月1日起开始“三天打鱼两天晒网”,即工作三天,然后再休息两天。问这个人在以后的某一天中是在工作还是在休息。从键盘任意输入一天,编程判断他是在工作还是在休息,如果是在工作,则输出:He is working,如果是在休息,则输出:He is having a rest,如果输入的年份小于1990或者输入的月份和日期不合法,则输出:Invalid input。

输入格式: "%4d-%2d-%2d"

输出格式:

"Invalid input" 或

"He is having a rest" 或

"He is working"

输入样例1:

2014-12-22

输出样例1:

He is working

输入样例2:

2014-12-24

输出样例2:

He is having a rest

输入样例3:

2014-12-32

输出样例3:

Invalid input

注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!

时间限制:500ms内存限制:32000kb

#include

int isLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

int getDaysInMonth(int year, int month) {
    int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && isLeapYear(year)) {
        return 29;
    }
    return daysInMonth[month - 1];
}

int isValidDate(int year, int month, int day) {
    return year >= 1990 && month >= 1 && month <= 12 && day >= 1 && day <= getDaysInMonth(year, month);
}

int calculateTotalDays(int year, int month, int day) {
    int totalDays = 0;
    for (int y = 1990; y < year; y++) {
        totalDays += isLeapYear(y) ? 366 : 365;
    }
    for (int m = 1; m < month; m++) {
        totalDays += getDaysInMonth(year, m);
    }
    totalDays += day - 1;
    return totalDays;
}

int main() {
    int year, month, day;
    scanf("%4d-%2d-%2d", &year, &month, &day);

    if (!isValidDate(year, month, day)) {
        printf("Invalid input");
        return 0;
    }

    int totalDays = calculateTotalDays(year, month, day);
    int cycleDay = totalDays % 5;

    if (cycleDay == 0 || cycleDay == 4) {
        printf("He is having a rest");
    } else {
        printf("He is working");
    }

    return 0;
}

 

你可能感兴趣的:(c语言,开发语言)