PHP
·
发表于 5年以前
·
阅读量:8410
以下是多位水仙花数的Go语言方法
package main
import (
"fmt"
"math"
"strconv"
"time"
)
func main() {
//水仙花数
t1 := time.Now()
var i int
for i = 1; i < 99999999; i++ {
var len1 int = len(strconv.Itoa(i))
var len2 int = len(strconv.Itoa(i))
var s [20]int
var sum = 0
for j := 0; j < len1; j++ {
s[j] = i / int(math.Pow10(len1-j-1)) % 10
sum = sum + int(math.Pow(float64(s[j]), float64(len2)))
}
if sum == i {
fmt.Println(i)
}
}
ttt := time.Since(t1)
fmt.Println("耗时:",ttt)
}