中国有句古话叫“三天打渔两天晒网”。某人从2020年1月1日开始三天打渔两天晒网(也就是1月的1号,2号,3号打渔,4号,5号休息,依次类推),输入任一2020年1月1日之后的日期(输入格式:年-月-日 例如:2022-4-20),判断此人当天是在打渔还是晒网。运行效果如下:
输入:
2022-4-19
输出:
sleeping!
输入:
2022-4-20
输出:
fishing!
switch~case语句实现
#include<stdio.h>
int main(void)
{
int i, j, k, N, a, b, t, m;
a = 0;
b = 0;
scanf("%d-%d-%d", &i, &j, &k);
N = k;
for (m = 2020; m < i; m++)
{
if (m % 400 == 0 || (m % 4 == 0 && m % 100 != 0))
a++;
else
b++;
}
N += a * 366 + b * 365;
for (t = 1; t < j; t++)
{
switch (t)
{
case 2:
if (i % 400 == 0 || (i % 4 == 0 && i % 100 != 0))
N += 29;
else
N += 28;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
N += 31;
break;
case 4:
case 6:
case 9:
case 11:
N += 30;
break;
}
}
if (N % 5 == 1 || N % 5 == 2 || N % 5 == 3)
printf("fishing!");
else
printf("sleeping!");
return 0;
}
数组实现:
#include <stdio.h>
int main(void)
{
int a1[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
int a2[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
int year, month, day;
int i, j;
scanf("%d-%d-%d", &year, &month, &day);
int sum = day;
for (i = 2020; i < year; i++)
for (j = 0; j < 12; j++)
{
if ((0 == i % 4 && i % 100 != 0) || (0 == i % 400))
sum += a1[j];
else
sum += a2[j];
}
for (i = 0; i < month - 1; i++) {
if((0 == year % 4 && year % 100 != 0) || (0 == year % 400))
sum += a1[i];
else
sum += a2[i];
}
if (sum % 5 == 1 || sum % 5 == 2 || sum % 5 == 3)
printf("fishing!");
else
printf("sleeping!");
return 0;
}