package main
import "fmt"
func main() {
var factorial int64 = 1
var number int
fmt.Print("请输入一个整型:")
fmt.Scanf("%d", &number)
for i := 1; i <= number; i++ {
factorial = factorial * int64(i)
}
fmt.Printf("%d的阶乘是%d\n", number, factorial)
}
2 回答
pardon110
TA贡献1038条经验 获得超227个赞
应该用递归,而非for循环,代码如下:
package main import "fmt" func factorial(n int) int { if n == 1 { return n } else { n = n * factorial(n-1) } return n } func main() { var number int fmt.Print("请输入一个整型:") fmt.Scanf("%d", &number) fmt.Printf("%d的阶乘是%d\n", number, factorial(number)) }
慕田峪4261139
TA贡献1条经验 获得超0个赞
#100的阶乘 from functools import reduce def fo(x, y): return x * y print(reduce(fo, range(1, 101)))
添加回答
举报
0/150
提交
取消