题解 | #简单密码#

简单密码

https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac

package main

import (
	"fmt"
    "strconv"
)

func main() {
	var input string
	fmt.Scan(&input)

	//建立一个map存放小写字母和数字的对应
	myMap := map[byte]int{
		'a': 2, 'b': 2, 'c': 2,
		'd': 3, 'e': 3, 'f': 3,
		'g': 4, 'h': 4, 'i': 4,
		'j': 5, 'k': 5, 'l': 5,
		'm': 6, 'n': 6, 'o': 6,
		'p': 7, 'q': 7, 'r': 7, 's': 7,
		't': 8, 'u': 8, 'v': 8,
		'w': 9, 'x': 9, 'y': 9, 'z': 9,
	}

	inputByt := []byte(input)
	var res []byte

	for _, val := range inputByt {
		_, ok := myMap[val]
		if ok {
            //这些操作是因为自己懒,其实一开始在map里面把数字存成byte就好了
			temp := myMap[val]
			tempS := strconv.Itoa(temp)
			tempByt := []byte(tempS)
			res = append(res, tempByt[0])
		} else if val >= 'A' && val <= 'Z' {
			p := UpEncrepty(val)
			res = append(res, p)
		} else {
			res = append(res, val)
		}
	}

	resS := string(res)

	fmt.Println(resS)
}

//编写一个函数得到大写字母的密码
func UpEncrepty(u byte) (p byte) {
	if u >= 'A' && u <= 'Y' {
		p = u + 32 + 1
	}

	if u == 'Z' {
		p = 'a'
	}

	return p
}

全部评论

相关推荐

昨天 13:52
武汉大学 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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