go 实现1000以内的数字,输入35 输出三十五

/*
1000以内的数字,输入35 输出三十五
 */

func test(i int) string {
	if i == 0 {
		return "不能是0"
	}
	newStrs := strconv.Itoa(i)
	var bt bytes.Buffer
	for i :=0; i<len(newStrs); i ++{
		if newStrs[i] == 0{
			continue
		}else {
			bt.WriteString(newStrs[i:])
			break
		}
	}
	newStr := bt.String()
	newStrLen := len(newStr)
	numMap := make(map[int]string)
	numMap[0] = "零"
	numMap[1] = "一"
	numMap[2] = "二"
	numMap[3] = "三"
	numMap[4] = "四"
	numMap[5] = "五"
	numMap[6] = "六"
	numMap[7] = "七"
	numMap[8] = "八"
	numMap[9] = "九"
	switch {
	case newStrLen == 1:
		return numMap[i]
	case newStrLen == 2:
		n1,_ := strconv.Atoi(string(newStr[0]))
		n2,_ := strconv.Atoi(string(newStr[1]))
		if string(newStr[0]) == "1" && string(newStr[1]) == "0"{
			return "十"
		}
		if string(newStr[1]) == "0"{
			return numMap[n1] + "十"
		}
		return numMap[n1] + "十" + numMap[n2]
	case newStrLen == 3:
		n1,_ := strconv.Atoi(string(newStr[0]))
		n2,_ := strconv.Atoi(string(newStr[1]))
		n3,_ := strconv.Atoi(string(newStr[2]))
		if string(newStr[1]) == "0"&& string(newStr[2]) == "0"{
			return numMap[n1] + "百"
		}
		if string(newStr[1]) == "0"{
			return numMap[n1] + "百" +numMap[n2] + numMap[n3]
		}
		return numMap[n1] + "百" +numMap[n2] + "十" + numMap[n3]
	}
	return "error"
}

func main()  {
	println(test(0))
	println(test(3))
	println(test(99))
	println(test(999))
	println(test(105))
	println(test(10))
	println(test(20))
	println(test(100))
	println(test(001))
}

  

 

posted @ 2021-10-19 17:31  绝世老中医  阅读(46)  评论(0编辑  收藏  举报