
C语言,判断是否闰年
3个回答
展开全部
if
(year
%
4
==
0
&&
year
%
100
!=
0
||
year
%
400
==
0)
{
//输出某年是闰年
System.out.println
(year
+
"年是闰年");
}
else
{
//输出某年是平年
System.out.println
(year
+
"年是平年");
}
1。能被4整除而不能被100整除。(如2004年就是闰年,1800年不是。)
2。能被400整除。(如2000年是闰年)
(year
%
4
==
0
&&
year
%
100
!=
0
||
year
%
400
==
0)
{
//输出某年是闰年
System.out.println
(year
+
"年是闰年");
}
else
{
//输出某年是平年
System.out.println
(year
+
"年是平年");
}
1。能被4整除而不能被100整除。(如2004年就是闰年,1800年不是。)
2。能被400整除。(如2000年是闰年)
展开全部
把判断的那行直接写在return后面,就可以封装成一个函数了。不过不建议这么做,没有太大的必要性,反而增加了函数寻址、压栈、出栈的时间消耗
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
按你的需求,可以用宏替换来做(当然也可以写成函数)
#define
isLeap(x)
((x)
%
4
==
0
?
(x)
%
100
==
0
?
(x)
%
400
==
0
?
1
:
0
:
1
:
0)
int
main(void)
{
int
i
=
0;
for
(i
=
2000;
i
<=
2050;
i
++)
{
if
(isLeap(i))
{
printf("%ld年是闰年\n",
i);
}
else
{
printf("%ld年不是闰年\n",
i);
}
}
}
#define
isLeap(x)
((x)
%
4
==
0
?
(x)
%
100
==
0
?
(x)
%
400
==
0
?
1
:
0
:
1
:
0)
int
main(void)
{
int
i
=
0;
for
(i
=
2000;
i
<=
2050;
i
++)
{
if
(isLeap(i))
{
printf("%ld年是闰年\n",
i);
}
else
{
printf("%ld年不是闰年\n",
i);
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询