题目
关于翻译
这道题翻译的像···(自己脑补)下面由我来分析一下题意:
每个数字都可以翻译成字母(除了“Q”和“Z”),如下:
2: A,B,C 5: J,K,L 8: T,U,V
3: D,E,F 6: M,N,O 9: W,X,Y
4: G,H,I 7: P,R,S
现在有4617个字符串,问其中有没有一个编号n可以翻译成的,如果有,则输出这个字符串,如果没有,则输出“NONE”。
举个例子:
n为4734
有10个字符串:
GREG
ABCD
YYYY
BYTE
CVBN
QAWE
PPKL
MMKJ
BBLO
ILLL
很显然,4734只能翻译成GREG,所以输出GREG
思路:
由于将编号翻译成字符串可能会超时,所以我们选择将字符串翻译成编号(需要特判“Q”和“Z”),这道题不用任何算法,直接模拟即可。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main(){
string n,m="";
string s;
int x=0;
cin>>n;
for(int i=1;i<=4617;i++){
m="";
cin>>s;
if(s.length()==n.length()){
for(int j=0;j<s.length();j++){
if(s[j]=='Q'||s[j]=='Z')break;
if(s[j]>='A'&&s[j]<='C'){
m+='2';
}if(s[j]>='D'&&s[j]<='F'){
m+='3';
}if(s[j]>='G'&&s[j]<='I'){
m+='4';
}if(s[j]>='J'&&s[j]<='L'){
m+='5';
}if(s[j]>='M'&&s[j]<='O'){
m+='6';
}if(s[j]>='P'&&s[j]<='S'){
m+='7';
}if(s[j]>='T'&&s[j]<='V'){
m+='8';
}if(s[j]>='W'&&s[j]<='Y'){
m+='9';
}if(m==n){
cout<<s<<endl;
x=1;
}
}
}
}if(x==0)cout<<"NONE";
return 0;
}