题目内容:
中国有句俗语叫“三天打鱼两天晒网”,某人从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
#include<stdio.h>
int isleap(int year){
return ( (year%4==0)&&(year%100!=0) )|| (year %400==0);}
int main(){
int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},
{31,31},{30,30},{31,31}};
int y1,m1,d1;
int y2,m2,d2;
y1=1990;m1=1;d1=1;
scanf("%4d-%2d-%2d",&y2,&m2,&d2);
int flag=isleap(y2);
if(month[m2][flag]<d2){
printf("Invalid input");
return 0; }
int count=1;
while(y1<y2||m1<m2||d1<d2){
d1++;
if(d1-1==month[m1][isleap(y1)]){
m1++;
d1=1;}//一个月遍历完了,重新开始一个月,从1号开始
if(m1==13){
m1=1;//重新从一月份开始
y1++;}//一年遍历完了,重新开始一年
count++;}
if(count%5==1||count%5==2||count%5==3){
printf("He is working");}
else printf("He is having a rest");
return 0;
}