题目:从键盘输入一个日期,判断是该年的第几天

这题考点很刁钻,读者可以用多种方法计算,考点为:

1.  如何判断是否为闰年

2.  计算二月份时,日期是否要 + 1

最常用的就是通过switch语句,难度为:1颗星

首先我们需要了解一下什么是闰年:

判断任意年份是否为闰年,需要满足以下条件中的任意一个:
① 该年份能被 4 整除同时不能被 100 整除;
② 该年份能被400整除。 

 

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int sum = 0;
 5     int year = 0;
 6     int month = 1;
 7     int day = 0;
 8 
 9     printf("please input the date(year-month-day):");
10     scanf_s("%d-%d-%d", &year, &month, &sum);
11 
12     switch (month - 1)    //故意没有在case里加break
13     {
14     case 11:
15         sum += 30;
16     case 10:
17         sum += 31;
18     case 9:
19         sum += 30;
20     case 8:
21         sum += 31;
22     case 7:
23         sum += 31;
24     case 6:
25         sum += 30;
26     case 5:
27         sum += 31;
28     case 4:
29         sum += 30;
30     case 3:
31         sum += 31;
32     case 2:
33         if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)   // 判断是否为闰年
34         {
35             sum += 29;
36         }
37         else
38         {
39             sum += 28;
40         }
41     case 1:
42         sum += 31;
43 
44     default:
45         break;
46     }
47     printf("This is the %d day of %d.\n", sum, year);
48 }

 

posted @ 2021-05-18 19:12  北圳南  阅读(163)  评论(0编辑  收藏  举报