实例5 判断是每年的第几天
问题描述:
通过键盘输入某年某月某日,判断这一天是这一年的第几天。
算法分析:
主要了解是怎么计算这个时间的就行(可以看看闰年的计算方法)。
代码实现:
/**
* 题目:判断是每年的第几天
* 作者:shelly
* 功能:输入年月日,计算是多少天
*/
#include <stdio.h>
/**
* 函数名:main.
* 功能:通过输入年月日,计算天数.
* 参数:int isLeap(是否闰年), sum(求和), year(年), month(月), day(日), countDay[12](天数).
* 返回值:0.
*/
int main(void)
{
int isLeap = 0; //是否闰年
int sum = 0; //统计天数
int year = 0, month = 0, day = 0;
int countDay[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
printf("输入year : month : day = ");
scanf("%d %d %d", &year, &month, &day);
if(0 == year%400 || (0 == year%4 && 0 != year%100))
isLeap = 1;
for(int i = 0; i < month; i++)
{
sum += countDay[i];
}
sum += isLeap;
printf("it is the %dth day.\n", sum);
return 0;
}
结果实现:
(上面只是小编思路,如果你有更好的思路可以自己上机尝试下。)