
1
def isLeapYear(year: Int): Boolean = {
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}
2
def calculateF(n: Int): Double = {
(1 to n).map(i => if (i % 2 == 1) 1.0 / i else -1.0 / i).sum
}
val result2 = calculateF(100)
println(result2)
3
def fibonacci(n: Int): List[Int] = {
if (n == 1) List(1)
else if (n == 2) List(1, 1)
else {
val fibs = scala.collection.mutable.ListBuffer(1, 1)
for (i <- 2 until n) {
fibs += fibs(i - 1) + fibs(i - 2)
}
fibs.toList
}
}
val result3 = fibonacci(20)
result3.foreach(println)
4
import scala.io.StdIn
def inputUntilLeapYear(): Unit = {
var year = 0
do {
print("请输入一个整数年份:")
year = StdIn.readInt()
} while (!((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
println(s"输入的闰年是:$year")
}
inputUntilLeapYear()