题目:输入年月日,计算该天是该年的第多少天?(请使用 case)
分析:月份天数分为三种
31天的有:1、3、5、7、8、10、12
30天的有:4、6、9、11
28/29天的是2月,需要进行闰年与否的判断
/**< 输入年月日,计算该天是该年的第多少天?(请使用 case) */
/**< 需要判断是否为闰年:可以被4整除,但不能被100整除;能被400整除 */
#include <stdio.h>
#include <stdlib.h>
int isLeapYear(int);
int main()
{
int year;
int mouth;
int day;
int num = 0;
printf("输入年份1900-2200\n");
scanf("%d",&year);
printf("输入月份1-12\n");
scanf("%d",&mouth);
printf("输入日期1-31\n");
scanf("%d",&day);
switch(mouth)
{
case 12: num += 30;
case 11: num += 31;
case 10: num += 30;
case 9: num += 31;
case 8: num += 31;
case 7: num += 30;
case 6: num += 31;
case 5: num += 30;
case 4: num += 31;
case 3: num += 28;
case 2: num += 31;
}
if(isLeapYear(year) && mouth>2)
num = num + 1;
num = num + day;
printf("%d年%d月%d日是这年的第%d天",year,mouth,day,num);
return 0;
}
int isLeapYear(int year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0)) //判断为闰年
{
return 1;
}
else return 0;
}
运行结果: