用js打出 判断闰年(算法:能被4整除且不能整除100的为闰年(如2004年就是闰年1901年不是闰年)或者能够被400整除的就是闰年)
时间: 2023-11-16 13:05:11 浏览: 73
function isLeapYear(year) {
if (year % 4 === 0 && year % 100 !== 0) {
return true;
} else if (year % 400 === 0) {
return true;
} else {
return false;
}
}
// example usage
console.log(isLeapYear(2004)); // true
console.log(isLeapYear(1901)); // false
相关问题
判断闰年(算法:能被4整除且不能整除100的为闰年(如2004年就是闰年1901年不是闰年)或者能够被400整除的就是闰年)代码
```
def is_leap_year(year):
if year % 4 == 0 and year % 100 != 0:
return True
elif year % 400 == 0:
return True
else:
return False
```
示例:
```
print(is_leap_year(2004)) # True
print(is_leap_year(1901)) # False
print(is_leap_year(2000)) # True
print(is_leap_year(2021)) # False
```
怎么判定为闰年(true)比如2004,1901平年(false),使用逻辑运算符 算法:能被4整除“且”不能整除100“或者”能整除400的为闰年
好的,根据您的要求,我可以回答您的问题。下面是怎样判断是否为闰年的算法:
如果一个年份可以被4整除,但不能被100整除,那么它是闰年。或者,如果一个年份可以被400整除,那么它也是闰年。其他情况下,它就是平年。
下面是用逻辑运算符来表示这个算法:
((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)
其中,% 表示求余数。如果这个表达式的结果是真,那么这就是一个闰年,否则就是平年。