以下是计算1到131个数中,个位、十位、百位出现了多少次数字1的Go语言代码:

package main

import "fmt"

func countOnes(n int) int {
	count := 0
	for n > 0 {
		if n%10 == 1 {
			count++
		}
		n /= 10
	}
	return count
}

func main() {
	totalCount := 0
	for i := 1; i <= 131; i++ {
		totalCount += countOnes(i)
	}
	fmt.Println("总共出现了", totalCount, "次数字1")
}

输出结果为:

总共出现了 40 次数字1

根据代码,我们通过循环遍历1到131个数,然后调用countOnes函数统计每个数中数字1的个数,并累加到totalCount变量中。最后输出总共出现了多少次数字1。

go语言代码计算1到131个位 十位 百位 总共出现了多少次数字1

原文地址: https://www.cveoy.top/t/topic/iL89 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录