中国有句俗语叫“三天打鱼两天晒网”,某人从1990年1月1日起开始“三天打鱼两天晒网”,即工作三天,然后再休息两天。问这个人在以后的某一天中是在工作还是在休息。从键盘任意输入一天,用c语言编程判断他是在工作还是在休息,如果是在工作,则输出:He is working,如果是在休息,则输出:He is having a rest,如果输入的年份小于1990或者输入的月份和日期不合法,则输出:Invalid input。 输入格式: "%4d-%2d-%2d" 输出格式: "Invalid input" 或
时间: 2023-12-14 13:37:10 浏览: 146
"He is working" 或 "He is having a rest"
以下是C语言代码实现:
```c
#include <stdio.h>
int main() {
int year, month, day;
int days_since_1990_01_01, rest_days;
if (scanf("%4d-%2d-%2d", &year, &month, &day) != 3) {
printf("Invalid input");
return 0;
}
if (year < 1990 || month < 1 || month > 12 || day < 1 || day > 31) {
printf("Invalid input");
return 0;
}
// 计算自1990年1月1日以来经过了多少天
days_since_1990_01_01 = (year - 1990) * 365 + (year - 1988) / 4; // 先算出年份差距和闰年个数
switch (month - 1) {
case 11: days_since_1990_01_01 += 30;
case 10: days_since_1990_01_01 += 31;
case 9: days_since_1990_01_01 += 30;
case 8: days_since_1990_01_01 += 31;
case 7: days_since_1990_01_01 += 31;
case 6: days_since_1990_01_01 += 30;
case 5: days_since_1990_01_01 += 31;
case 4: days_since_1990_01_01 += 30;
case 3: days_since_1990_01_01 += 31;
case 2: days_since_1990_01_01 += year % 4 == 0 ? 29 : 28; // 判断闰年
case 1: days_since_1990_01_01 += 31;
case 0: days_since_1990_01_01 += day - 1;
}
// 计算休息天数
rest_days = days_since_1990_01_01 % 5;
// 判断工作/休息
if (rest_days >= 3) {
printf("He is having a rest");
} else {
printf("He is working");
}
return 0;
}
```
阅读全文