生活随笔
收集整理的這篇文章主要介紹了
NYOJ 643 发短信 暴力求解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
發短信
時間限制:
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 ");elseprintf("%d ",s[str[i]-'A']);}}printf("\n");}return 0;
}
總結
以上是生活随笔為你收集整理的NYOJ 643 发短信 暴力求解的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。