打印1000年—2000年之间的闰年

为什么会有闰年可以详细的去了解一下,对于这道题的理解也会更透彻一些;
了解之后会发现闰年的规律:
四年一闰,百年不闰,四百年再闰;
源代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int i = 0;
	int count = 0;
	for (i = 1000; i <= 2000; i++)
	{
		if (i % 4 == 0)
		{
			if (i % 100 != 0)
				printf("%d ", i);
				count++;
		}
		if (i % 400 == 0)
		{
			printf("%d ", i);
			count++;
		}
	}
	printf("\n");
	printf("%d\n", count);
	system("pause");
	return 0;
}

代码的优化:

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int year = 0;
	for (year = 1000; year <= 2000; year++)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
		{
			printf("%d ", year);
		}
	}
	printf("\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tomatolee221/article/details/84729448