Problem G: 锁屏密码

发布时间 2023-04-29 10:27:38作者: hangsingplus

Problem Description

在九宫格键盘中,数字和字母在一个按键上面,其中数字2对应的字母是”abc”,3对应的是”def”,4对应的是”ghi”,5对应的是”jkl”,6对应的是”mno”,7对应的是”pqrs”,8对应的是”tuv”,9对应的是”wxyz”。
首先有一个仅包含小写字母的明文密码,如字母tmj,然后,按照九宫格键盘上字母的标注,将每个字母转化为相应的数字按键。如t出现在按键8上,那么其对应的数字为8,则明文密码”tmj”对应的密码为865。
你的任务很简单,就是根据给你的一串明文字母,按照上述规则转化成数字密码。

Input Description

每个测试样例第一行为一个正整数n(n<=10)表示密码的长度。当n为0时,测试结束。
接下来为一行长度为n的仅由小写字母组成的字符串,代表明文密码。

Output Description

每一个样例对应的输出为一行,输出对应的数字密码。

Sample Input

3 
tmj
6
jarily
0

Sample Output

865
527459

ac代码:

 1 def func():
 2     while True:
 3         try:
 4             n = int(input())
 5             if n==0:
 6                 break
 7             x = []
 8             x = input()
 9             x = list(x)
10             decrypt(x,n)
11         except EOFError:
12             break
13 def find(x):
14     if x>='a' and x<='c':
15         return 2
16     if x>='d' and x<='f':
17         return 3
18     if x>='g' and x<='i':
19         return 4
20     if x>='j' and x<='l':
21         return 5
22     if x>='m' and x<='o':
23         return 6
24     if x>='p' and x<='s':
25         return 7
26     if x>='t' and x<='v':
27         return 8
28     if x>='w' and x<='z':
29         return 9
30 def decrypt(x,n):
31     code = [] #接受密码
32     for i in range(n):
33         ans = find(x[i])
34         print(int(ans),end='')
35     print()
36 if __name__ == '__main__':
37     func()