PTA 模拟【string以及字母->数组下标】

假设有九宫格输入法键盘布局如下:
[ 1,.?! ] [ 2ABC ] [ 3DEF ]
[ 4GHI ] [ 5JKL ] [ 6MNO ]
[ 7PQRS ] [ 8TUV ] [ 9WXYZ ]
[ 0空 ]

注意:中括号[ ]仅为了表示键盘的分隔,不是输入字符。每个中括号中,位于首位的数字字符即是键盘的按键,按一下即可输入该数字字符。多次按同一个键,则输入的字符依次循环轮流,例如按两次3,则输入D;按5次7,则输入S;按6次2,则输入A。按键0的输入组合是0和空格字符,即按两次0输入空格。

你需要对于给定的按键组合,给出该组合对应的文本。

输入格式:

输入在一行中给出数个字符的按键组合(例如 999 表示按3次9),每个字符的按键组合之间用空格间隔,最后一个输入法组合之后以换行结束。输入数据至少包括一个字符的按键组合,且输入总长度不超过500个字符。

输出格式:

在一行中输出该按键组合对应的文本。

输入样例:
22 5555 22 666 00 88 888 7777 4444 666 44

输出样例:
ALAN TURING

厂长的代码。。。。。。好看。。。

string s[]={"0 ", "1,.?!", "2ABC", "3DEF", "4GHI", "5JKL", "6MNO", "7PQRS", "8TUV", "9WXYZ"};
char ss[505];
int main()
{
    while(~scanf("%s", ss))
    {
        int n=strlen(ss)-1;
        n%=s[ss[0]-'0'].length();
        putchar(s[ss[0]-'0'][n]);
    }
    puts("");
    return 0;
}

我的代码。。懒得改了//
。。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PI;
typedef pair< PI, int> PII;
const double eps=1e-5;
const double pi=acos(-1.0);
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const int MAXN=1100;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1


const int N=1e4+10;

int n;

char s[N];
char ans[N];
void solve(char c,int num)
{
    if(c=='0'){
        if(num==1)
        ans[n++]='0';
        else
        ans[n++]=' ';
    }
    else if(c=='1'){
        if(num==1){
            ans[n++]='1';
        }
        else if(num==2){
            ans[n++]=',';
        }
        else if(num==3){
            ans[n++]='.';
        }
        else if(num==4){
            ans[n++]='?';
        }
        else if(num==0){
            ans[n++]='!';
        }
    }
    else if(c=='2'){
        if(num==1){
            ans[n++]='2';
        }
        else if(num==2){
            ans[n++]='A';
        }
        else if(num==3){
            ans[n++]='B';
        }
        else if(num==0){
            ans[n++]='C';
        }
    }
    else if(c=='3'){
        if(num==1){
            ans[n++]='3';
        }
        else if(num==2){
            ans[n++]='D';
        }
        else if(num==3){
            ans[n++]='E';
        }
        else if(num==0){
            ans[n++]='F';
        }
    }
    else if(c=='4'){
        if(num==1){
            ans[n++]='4';
        }
        else if(num==2){
            ans[n++]='G';
        }
        else if(num==3){
            ans[n++]='H';
        }
        else if(num==0){
            ans[n++]='I';
        }
    }
    else if(c=='5'){
        if(num==1){
            ans[n++]='5';
        }
        else if(num==2){
            ans[n++]='J';
        }
        else if(num==3){
            ans[n++]='K';
        }
        else if(num==0){
            ans[n++]='L';
        }
    }
    else if(c=='6'){
        if(num==1){
            ans[n++]='6';
        }
        else if(num==2){
            ans[n++]='M';
        }
        else if(num==3){
            ans[n++]='N';
        }
        else if(num==0){
            ans[n++]='O';
        }
    }
    else if(c=='7'){
        if(num==1){
            ans[n++]='7';
        }
        else if(num==2){
            ans[n++]='P';
        }
        else if(num==3){
            ans[n++]='Q';
        }
        else if(num==4){
            ans[n++]='R';
        }
        else if(num==0){
            ans[n++]='S';
        }
    }
    else if(c=='8'){
        if(num==1){
            ans[n++]='8';
        }
        else if(num==2){
            ans[n++]='T';
        }
        else if(num==3){
            ans[n++]='U';
        }
        else if(num==0){
            ans[n++]='V';
        }
    }
    else if(c=='9'){
        if(num==1){
            ans[n++]='9';
        }
        else if(num==2){
            ans[n++]='W';
        }
        else if(num==3){
            ans[n++]='X';
        }
        else if(num==4){
            ans[n++]='Y';
        }
        else if(num==0){
            ans[n++]='Z';
        }
    }
}

int main()
{
    gets(s);
    int len;
    char c;
    int num,flag;
    len=strlen(s);
    num=flag=n=0;
    for(int i=0;i<len;i++){
        if(s[i]!=' '){
            c=s[i];
            num++;
            flag=1;
        }
        else if(s[i]==' '){
            if(c=='1'||c=='7'||c=='9')
                num%=5;
            if(c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='8')
                num%=4;
            if(c=='0')
                num%=2;
            solve(c,num);
            num=0;
            flag=0;
        }
    }
    if(flag){
        if(c=='1'||c=='7'||c=='9')
            num%=5;
        if(c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='8')
            num%=4;
        if(c=='0')
            num%=2;
        solve(c,num);
    }
    ans[n]='';
    printf("%s",ans);
    return 0;
}
/*
22 5555555555 22 666 00 88 888 7777 4444 666 44
*/
原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934455.html