版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lhtzbj12/article/details/78941368
前几天跟同事聊天时提到了他之前遭遇的一道面试题:求1000以下出现1的次数。今天带娃时,构思了一下,有电脑时将自己的想法写了一下来。
package main
import (
"strconv"
"fmt"
)
func main() {
num := 0
for i := 0; i < 1000; i++ {
str := strconv.Itoa(i)
length := len(str)
for j := 0; j < length; j++ {
if str[j] == '1' {
num++
}
}
}
fmt.Println(num)
}