定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。(算法学习第17天,C代码)

2020.5.8

学习的第17天

这几天比较苦呀,大晚上写代码~~

题目

定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。

例如
输入
2000 12 31
输出
366

C代码

#include
struct time 
{
	int year;
	int mouth;
	int day;
};
int main()
{
	struct time a;
	int i, d = 0;
	scanf("%d%d%d",&a.year,&a.mouth,&a.day);
	for (i = 1; i < a.mouth; i++)
	{
		if (i == 2)
		{
			if ((a.year % 4 == 0 && a.year % 100 != 0) || a.year % 400 == 0)
				d += 29;
			else
				d += 28;
			continue;
		}		
		if (i % 2 == 0)
			d += 30;
		else
			d += 31;
	}
	d += a.day;
	printf("%d",d);
	return 0;
}

我这代码测试没问题,但是在提交却说答案错误。就挺纳闷的~~~

如有错误恳请大佬指点,感激不尽

算法题目来源:https://www.dotcpp.com/

你可能感兴趣的:(算法学习,算法)