0x00:前言
- 实现一个函数,参数为n,返回1(1+2)…*(1+2+…+n)的值
0x01:代码
package main
import "fmt"
func getSum(n int) (sum int) {
sum := 1
for i := 1; i <= n; i++ {
sum1 := 0
for j := 1; j <= i; j++{
sum1 += j
}
sum = sum*sum1
}
return
}
func main(){
for i := 1; i <= 5; i++ {
fmt.Printf("n=%d,result:%d",i,getSum(i))
}
}
兄弟写的非常好 https://www.cscnn.com/