【编程题目 |200分】 九宫格按键输入法【2021 H2, 2022 Q1,Q2 考试题】


时间限制:C/C++ 1秒,其他语言 2秒

空间限制:C/C++262144K,其他语言524288K

64bit IO Format:%lld


本题可使用本地IDE编码,不能使用本地已有代码,无跳出限制,

编码后请点击”保存并调试“按钮进行代码提交。


题目描述

九宫格按键输入,判断输出,有英文和数字两个模式,默认是数字模式,数字模式直接输出数字,英文模式连续按同一个按键会依次出现这个按键上的字母,如果输入”/”或者其他字符,则循环中断。

要求输入一串按键,输出屏幕显示。


输入描述:

输入范围为数字 0~9 和字符’#’、’/’,输出屏幕显示,例如,

在数字模式下,输入 1234,显示 1234

在英文模式下,输入 1234,显示,adg

输出描述:

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

示例 1 输入输出示例仅供调试,后台判题数据一般不包含示例

输入

123#222235/56

输出

123adjjm



代码实现


C++解法一


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
const string NineGridInputMethod(const string &input)
{
string output;
size_t strLen = input.length();
int i = 0;
#include <bits/stdc++.h> using namespace std; class Solution { public: const string NineGridInputMethod(const string &input) { string output; size_t strLen = input.length(); int i = 0;
#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    const string NineGridInputMethod(const string &input)
    {
        string output;
        size_t strLen = input.length();

        int i = 0;

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

C++解法二


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<bits/stdc++.h>
using namespace std;
int main()
{
string input_str;
cin >> input_str;
#include<bits/stdc++.h> using namespace std; int main() { string input_str; cin >> input_str;
#include<bits/stdc++.h>

using namespace std;

int main() 
{
    string input_str;
    cin >> input_str;

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JAVA


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import java.util.Scanner;
public class Main {
public static String temp = "";
public static StringBuffer sb = new StringBuffer();
public static int count = 0;
import java.util.Scanner; public class Main { public static String temp = ""; public static StringBuffer sb = new StringBuffer(); public static int count = 0;
import java.util.Scanner;

public class Main {

    public static String temp = "";   
    public static StringBuffer sb = new StringBuffer();   
    public static int count = 0;

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

Python


Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Solution:
def NineGridInputMethod(self, input_str):
dic = {'1': ',.', '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
'6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
num_mode = True
length = len(input_str)
class Solution: def NineGridInputMethod(self, input_str): dic = {'1': ',.', '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'} num_mode = True length = len(input_str)
class Solution:
    def NineGridInputMethod(self, input_str):
        dic = {'1': ',.', '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl',
               '6': 'mno', '7': 'pqrs', '8': 'tuv', '9': 'wxyz'}
        num_mode = True  
        length = len(input_str)

剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看

JavaScript


剩余50%内容,订阅会员后查看


隐藏内容

此处内容需要权限查看

  • 普通用户特权:11金币
  • 会员用户特权:免费
  • 永久会员用户特权:免费推荐
会员免费查看