九宫格按键输入法(华为od机考题目3)

方法一(Python语言):

keyboard = {
    1:',.',
    2:'abc',
    3:'def',
    4:'ghi',
    5:'jkl',
    6:'mno',
    7:'pqrs',
    8:'tuv',
    9:'wxyz',
    0:' '
}

def fun(s):
    result = []
    mode = 1                            # 1:数字  0:字母
    tmpL = list(s)
    stack = []

    def out(mode):
        nonlocal stack
        if mode:
            while stack:
                result.append(stack.pop(0))
        else:
            lenStack = len(stack)
            lenMap = len(keyboard[int(stack[-1])])
            result.append(keyboard[int(stack[-1])][(lenStack % lenMap) - 1])
            stack = []

    while tmpL or stack:
        if not tmpL:        # 只有stack非空
            out(mode)
        else:               # 只有tmpL非空、tmpL和stack均非空
            if tmpL[0] == '#':
                out(mode)
                tmpL.pop(0)
                mode = 0
            elif tmpL[0] == '/':
                out(mode)
                tmpL.pop(0)
            else:
                if not stack or tmpL[0] == stack[-1]:        # 多次相同的输入需要入栈
                    stack.append(tmpL.pop(0))
                else:                           # 输入不同则需要出栈计算之前输入的内容
                    out(mode)

    return ''.join(result)


if __name__ == '__main__':
    test = '#44882933444'
    print(fun(test))

方法二(Python语言):

import sys
def func(s):
    mode, res = None, ''
    if '#' in s:
        mode = s[0]
        num = s[1:]
    else:
        num = s
    dic = {1:',.', 2:'abc', 3:'def', 4:'ghi', 5:'jkl',
          6:'mno', 7:'pqrs', 8:'tuv', 9:'wxyz', 0:' '}
    if mode is None:
        if '/' in num:
            res = num.replace('/', '')
        else:
            res = num
    else:
        d = {}
        if '/' in num:
            spt = num.split('/')
            ch1, ch2 = spt[0], spt[1]
            for key in ch1:
                d[key] = d.get(key, 0) + 1
            for key in d:
                idx = d[key] % len(dic[int(key)]) - 1
                res += dic[int(key)][idx]
            d.clear()
            for key in ch2:
                d[key] = d.get(key, 0) + 1
            for key in d:
                idx = d[key] % len(dic[int(key)]) - 1
                res += dic[int(key)][idx]
 
        else:
            for key in num:
                d[key] = d.get(key, 0) + 1
            for key in d:
                idx = d[key] % len(dic[int(key)]) - 1
                res += dic[int(key)][idx]
    return res
 
if __name__ == '__main__':
    # s = sys.stdin.readline().strip()
    # s = '#2222/22'        # ab
    # s = '#222233'      # ae
    # s = '#222/233'       # cae
    # s = '2222/22'      # ab
    s = '1222222'       # 1222222
    print(func(s))

方法三(C++语言):

#include
#include
 
using namespace std;
char a[][7] = { " 0 ","1,..","2ABC","3DEF","4GHI","5JKL","6MNO","7PQRS","8TUV","9WXYZ" };
int main(int argc, char** argv)
{
    char b[505];
    int i;
    int len;
    int num, k = 0; int sum1= 1;
    string s;
    getline(cin, s);
    int c = s.find('#');
    if (c == -1)
        c = s.length();
    len = s.length();
    if (c != s.length()) {
        string s2 = s.substr(c + 1, s.length());
        int len2 = s2.length();
        for (int m = 0; m < c; m++) {
            cout << s[m];
        }
        for (int j = 0; j < len2; j++)
        {
            if (s2[j] == '/')
                continue;
            if (s2[j] == s2[j + 1]) {
                sum1 += 1;
            }
            else {
                s2[j] = s2[j] - '0';
                num = sum1 % 3;
                if (num == 0) num = 3;
                cout << a[s2[j]][num];
                sum1 = 1;
            }
 
        }
    }
    else
    {
        for (int n = 0; n < c; n++) {
            cout << s[n];
        }
    }
    
    
    return 0;
}

题目描述:

第三道题目:九宫格按键输入

九宫格按键输入,输出显示内容,有英文和数字两种模式:默认是数字模式,当输入以#开头是切换为字母模式,当输入中包含“/”时,则循环中断,且“/”仅适用于字母模式,对数字模式无效。字符对应关系如图。
要求输入一串按键,输出屏幕显示内容。

输入描述:
输入范围为数字 0~9 和字符 ‘#’、’/’,输出屏幕显示,例如:
在数字模式下,输入 1234,显示 1234
在英文模式下,输入 1234,显示,adg

输出描述:
#用于切换模式,默认是数字模式,执行 # 后切换为英文模式;
/表示延迟,例如在英文模式下,输入 22/222,显示为 bc;
英文模式下,多次按同一键,例如输入 22222,显示为 b;

示例1:输入
2222/22
输出:
222222
示例2:输入
#2222/22
输出:
ab
示例3:
输入:
#222233
输出:
ae

你可能感兴趣的:(华为,算法,面试)