用C语言编程:某人三天打渔两天晒网,假设他从1990年1月1日开始打渔三天,然后晒网两天,请编程回答任意的一天他在打渔还是晒网。 A boy works for 3 days while has a 2 days off. If he is working on 1st, Jan, 1990, then for a date entered from the keyboard, please write a program to determine what the boy is doing, working or resting? Examples of input and output: 1)Input: 1990-01-05 Output: He is having a rest. 2)Input: 1990-01-07 Output: He is working. 3)Input: 1990-01-33 Output: Invali input. ***输入数据格式***d:"%4d-%2d-%2d" ***输出数据格式***:"Invalid input."或"He is having a rest." 或"He is working."
时间: 2023-06-24 10:07:35 浏览: 90
```c
#include <stdio.h>
int main() {
int year, month, day;
int days;
printf("Please enter a date (yyyy-mm-dd): ");
if (scanf("%d-%d-%d", &year, &month, &day) != 3) {
printf("Invalid input.\n");
return 0;
}
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
default:
printf("Invalid input.\n");
return 0;
}
if (day > days) {
printf("Invalid input.\n");
return 0;
}
int days_since_1990 = 0;
for (int y = 1990; y < year; y++) {
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
days_since_1990 += 366;
} else {
days_since_1990 += 365;
}
}
for (int m = 1; m < month; m++) {
switch (m) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days_since_1990 += 31;
break;
case 4: case 6: case 9: case 11:
days_since_1990 += 30;
break;
case 2:
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days_since_1990 += 29;
} else {
days_since_1990 += 28;
}
break;
}
}
days_since_1990 += day - 1;
int remainder = days_since_1990 % 5;
if (remainder < 3) {
printf("He is working.\n");
} else {
printf("He is having a rest.\n");
}
return 0;
}
```
阅读全文