AquaRio @ 2020-04-12 19:47:10
最近在学Go语言...
你看看这个和第一篇题解一模一样,为啥就是不对列
被卡精度了吗???
package main
import "fmt"
func main() {
n := 0
var k int
fmt.Scanf("%d", &k)
for Sn := 0.0; Sn <= float64(k); n, Sn = n+1, Sn+1.0/float64(n) {
}
fmt.Println(n)
}
by xrdrsp @ 2020-04-12 19:49:31
go看不懂~
by AquaRio @ 2020-04-12 19:50:49
@Yuan_Gao 别这样啊 真的是一样的啊
by 打表大蒟蒻 @ 2020-04-12 19:53:02
与题解一模一样(我发现了什么)
by AquaRio @ 2020-04-12 19:59:26
这份代码可以通过
package main
import "fmt"
func main() {
n := 0
Sn := 0.0
var k int
fmt.Scanf("%d", &k)
for Sn <= float64(k)+0.000001 {
n = n + 1
Sn = Sn + 1.0/float64(n)
}
fmt.Println(n)
}
by AquaRio @ 2020-04-12 20:04:04
n, Sn = n+1, Sn+1.0/float64(n)
这句话出现了问题,float64(n)
里的n
并没有变成n+1
,导致Sn
变成了+INF
by Franka @ 2020-04-12 20:10:15
不懂