题解 | #简单密码#
简单密码
https://www.nowcoder.com/practice/7960b5038a2142a18e27e4c733855dac
package main import ( "fmt" "os" "bufio" ) func transform(s string) string { m := make(map[byte]byte, 0) m['a'] = '2'; m['b'] = '2'; m['c'] = '2' m['d'] = '3'; m['e'] = '3'; m['f'] = '3' m['g'] = '4'; m['h'] = '4'; m['i'] = '4' m['j'] = '5'; m['k'] = '5'; m['l'] = '5' m['m'] = '6'; m['n'] = '6'; m['o'] = '6' m['p'] = '7'; m['q'] = '7'; m['r'] = '7'; m['s'] = '7' m['t'] = '8'; m['u'] = '8'; m['v'] = '8' m['w'] = '9'; m['x'] = '9'; m['y'] = '9'; m['z'] = '9' size := len(s) sb := make([]byte, size) for i:=0; i<size; i++ { ch := s[i] if 'a' <= s[i] && s[i] <= 'z' { ch = m[s[i]] } else if 'A' <= s[i] && s[i] <= 'Z' { ch = byte(s[i] + 32) if ch == 'z' { ch = 'a' } else { ch = ch + 1 } } else { ch = s[i] } sb[i] = ch } return string(sb) } func main() { var s string inputReader := bufio.NewReader(os.Stdin) line, _, _ := inputReader.ReadLine() s = string(line) // fmt.Println(s) fmt.Println(transform(s)) }
// 本题输入一行字符串,所以使用:inputReader.ReadLine()