题解 | #表示数字#

表示数字

https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
    scan:=bufio.NewScanner(os.Stdin)
    scan.Scan()
    input:=scan.Text()
    var res []rune
    // 扫描,对数字部分进行特殊处理
    for i,v:=range input{
        if v>='0'&&v<='9'{
		  //对数字字符,index==0或者前一位是其他字符,加个*
            if i==0||(input[i-1]<'0'||input[i-1]>'9'){
                res = append(res, '*')
            }
		  //添加本体
            res = append(res, v)
		  //如果index是最后一个或者后一位是其他字符,加个*
            if i==len(input)-1||(input[i+1]<'0'||input[i+1]>'9'){
                res = append(res, '*')
            }
            
        }else{
            res = append(res, v)
        }
    }
    fmt.Println(string(res))
}

全部评论

相关推荐

不知道怎么取名字_:现在找工作是真的太不容易了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务