使用编程语言输出1900年~2000年的全部闰年
闰年的判断规则:
- 普通闰年:公历年份是4的倍数,且不是100的倍数的,为闰年(如2004年、2020年等就是闰年)。
- 世纪闰年:公历年份是整百数的,必须是400的倍数才是闰年(如1900年不是闰年,2000年是闰年)。
Python语言:
def leap_year():
# 定义一个列表装是闰年的年份
year_list = []
# 循环遍历,并把是闰年的年份添加到‘year_list’列表内
for year in range(1900, 2001):
if year % 4 == 0 and year % 100 != 0:
year_list.append(year)
elif year % 400 == 0:
year_list.append(year)
# print(year_list)
return year_list
if __name__ == "__main__":
leap_year()
TypeScript语言:
function leapYear(){
// 定义一个数组装是闰年的年份
let yearList: number[] = []
// 循环遍历,并把是闰年的年份添加到‘year_list’数组内
for (let year: number = 1900; year <= 2001; year++){
if (year % 4 == 0 && year % 100 != 0){
yearList[yearList.length] = year
} else if (year % 400 == 0){
yearList[yearList.length] = year
}
}
// console.log(yearList)
return yearList
}
leapYear()
Go 语言
package main
import "fmt"
func main() {
fmt.Println(Array())
}
func Array() []int {
// 定义一个数组装是闰年的年份
var yearList []int
i := 0
for year := 1900; year < 2001; year++ {
// 判断是否是闰年
if year % 400 == 0 {
// 扩宽数组长度
yearList = append(yearList, i)
// 插入闰年年份
yearList[i] = year
i++
} else if year % 100 != 0 && year % 4 == 0 {
// 扩宽数组长度
yearList = append(yearList, i)
// 插入闰年年份
yearList[i] = year
i++
}
}
//fmt.Println(yearList)
return yearList
}
Java 语言
package main;
import java.util.*;
public class test {
public static void main(String[] args) {
System.out.println(Array());
}
public static List<Integer> Array() {
// 定义一个数组装是闰年的年份
List<Integer> list = new ArrayList<>(0);
for (int newYear = 1900; newYear < 2001; newYear++) {
// 判断是否是闰年
if (newYear % 400 == 0) {
// 数组插入闰年数据
list.add(newYear);
} else if (newYear % 100 != 0 && newYear % 4 ==0) {
// 数组插入闰年数据
list.add(newYear);
}
}
// System.out.println(list);
return list;
}
}