现在的位置: 首页 > 综合 > 正文

NYOJ 643 发短信 暴力求解

2013年05月18日 ⁄ 综合 ⁄ 共 3202字 ⁄ 字号 评论关闭

发短信

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
下图是手机常用的九键英文输入法界面,如果要输入字母'A',我们只
需要按一次数字键2,按键顺序记为2;如果要输入字母'B'的话,我们需要连续按两次数字键2,按键顺序记为22;同理:字母'C’需要连续按3次数字键2,按键顺序记为222。通过这种方法,我们用手机10多个键就能输入26个英文字母。
现在你的任务是统计一段英文用手机输入的按键顺序,同样,你也要能把按键顺序翻译成相应的英文内容。
为了使问题简化,我们假设内容只有大写英文字母和空格。
输入
有多组测试数据
每组测试数据占一行,有两种情况:
(1)短信内容(只含有若干个空格和大写字母,不超过1000个字符)
(2)短信按键顺序(只含有若干空格和数字,其中第一个肯定是数字,不超过1000个字符)
输出
对于每组测试数据:
如果是短信内容,输出每个字母的按键顺序,每个字母的按键顺序用空格隔开
如果是按键顺序,输出它代表的内容
样例输入
I LOVE YOU
HELLO WORLD
444 0 555 666 888 33 0 999 666 88
44 33 555 555 666 0 9 666 777 555 3
样例输出
444 0 555 666 888 33 0 999 666 88
44 33 555 555 666 0 9 666 777 555 3
I LOVE YOU
HELLO WORLD
解法一:纯暴力求解。
 
#include<stdio.h>
#include<string.h>
int main()
{
    char str[1500],s[10];
    int i,len,j;
    while(gets(str)!=NULL)
    {
        len=strlen(str);
        if((str[0]>='A'&&str[0]<='Z')||str[0]==' ')
        {
            for(i=0;i<len;i++)
            {
                if(i) printf(" ");
                if(str[i]=='A') printf("2");
                else if(str[i]=='B') printf("22");
                else if(str[i]=='C') printf("222");
                else if(str[i]=='D') printf("3");
                else if(str[i]=='E') printf("33");
                else if(str[i]=='F') printf("333");
                else if(str[i]=='G') printf("4");
                else if(str[i]=='H') printf("44");
                else if(str[i]=='I') printf("444");
                else if(str[i]=='J') printf("5");
                else if(str[i]=='K') printf("55");
                else if(str[i]=='L') printf("555");
                else if(str[i]=='M') printf("6");
                else if(str[i]=='N') printf("66");
                else if(str[i]=='O') printf("666");
                else if(str[i]=='P') printf("7");
                else if(str[i]=='Q') printf("77");
                else if(str[i]=='R') printf("777");
                else if(str[i]=='S') printf("7777");
                else if(str[i]=='T') printf("8");
                else if(str[i]=='U') printf("88");
                else if(str[i]=='V') printf("888");
                else if(str[i]=='W') printf("9");
                else if(str[i]=='X') printf("99");
                else if(str[i]=='Y') printf("999");
                else if(str[i]=='Z') printf("9999");
                else if(str[i]==' ') printf("0");
            }
        }
        else
        {
            for(i=0,j=0;i<=len;i++)
            {
                if(str[i]!=' '&&i!=len)
                    s[j++]=str[i];
                else
                {
                    s[j]='\0';
                    if(!strcmp(s,"0")) printf(" ");
                    else if(!strcmp(s,"2")) printf("A");
                    else if(!strcmp(s,"22")) printf("B");
                    else if(!strcmp(s,"222")) printf("C");
                    else if(!strcmp(s,"3")) printf("D");
                    else if(!strcmp(s,"33")) printf("E");
                    else if(!strcmp(s,"333")) printf("F");
                    else if(!strcmp(s,"4")) printf("G");
                    else if(!strcmp(s,"44")) printf("H");
                    else if(!strcmp(s,"444")) printf("I");
                    else if(!strcmp(s,"5")) printf("J");
                    else if(!strcmp(s,"55")) printf("K");
                    else if(!strcmp(s,"555")) printf("L");
                    else if(!strcmp(s,"6")) printf("M");
                    else if(!strcmp(s,"66")) printf("N");
                    else if(!strcmp(s,"666")) printf("O");
                    else if(!strcmp(s,"7")) printf("P");
                    else if(!strcmp(s,"77")) printf("Q");
                    else if(!strcmp(s,"777")) printf("R");
                    else if(!strcmp(s,"7777")) printf("S");
                    else if(!strcmp(s,"8")) printf("T");
                    else if(!strcmp(s,"88")) printf("U");
                    else if(!strcmp(s,"888")) printf("V");
                    else if(!strcmp(s,"9")) printf("W");
                    else if(!strcmp(s,"99")) printf("X");
                    else if(!strcmp(s,"999")) printf("Y");
                    else if(!strcmp(s,"9999")) printf("Z");
                    j=0;
                }
            }
        }
        printf("\n");
    }
    return 0;
}

解法二:

 
#include<stdio.h>
#include<string.h>
int main()
{
    int s[28]={2,22,222,3,33,333,4,44,444,5,55,555,6,66,666,7,77,777,7777,8,88,888,9,99,999,9999};
    char str[1010];
    int i,j,sum,len;
    while(gets(str)!=NULL)
    {
        len=strlen(str);
        if(str[0]>='0'&&str[0]<='9')
        {
            sum=0;
            for(i=0;i<=len;i++)
            {
                if(str[i]!=' '&&str[i]!='\0')
                    sum=sum*10+(str[i]-'0');
                else
                {
                    if(sum==0)
                        printf(" ");
                    else
                    {
                        for(j=0;j<26;j++)
                        {
                            if(s[j]==sum)
                            {
                                printf("%c",j+'A');
                                break;
                            }
                        }
                        sum=0;
                    }
                }
            }
        }
        else
        {
            for(i=0;i<len;i++)
            {
                if(str[i]==' ')
                    printf("0 ");
                else
                    printf("%d ",s[str[i]-'A']);
            }
        }
        printf("\n");
    }
    return 0;
}    
【上篇】
【下篇】

抱歉!评论已关闭.