水题------找准字符串,输出前一项
common typing error is to place thehands on the keyboard one row to theright of the correct position. So `Q' istyped as `W' and `J' is typed as `K' andso on. You are to decode a message typed in this manner.
Input
Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (exceptQ,A,Z), or punctuation shown above [except back-quote (`)]. Keys labelled with words [Tab,BackSp,Control, etc.] are not represented in the input.
Output
You are to replace each letter or punction symbol by the one immediately to its left on the `QWERTY'keyboard shown above. Spaces in the input should be echoed in the output.Sample InputO S, GOMR YPFSU/Sample OutputI AM FINE TODAY.
题解:该题是假设输入一个键盘上的字符,如果该字符左侧是非输出字符,则输出该字符;否则,输出其左侧字符
解析:键盘上字符过多,可以考虑到将键盘上的字符改写成字符串,然后再找出该字符序数是否在字符串中(该题不考虑Q,A,Z,如果考虑的话建议另设一个字符串)
字符串的定义方式
1.char s[]="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";//建议第一种,第二种还需考虑转义序列
2.char ch1[50]={'`','1','2','3','4','5','6','7','8','9','0','-','=','Q','W','E','R','T','Y','U','I','O','P','[',']','\\','A','S','D','F','G','H','J','K','L',';','\'','Z','X','C','V','B','N','M',',','.','/'};
ac代码:
#include<iostream>
#include<cstdio>
char ch1[50]={'`','1','2','3','4','5','6','7','8','9','0','-','=','Q','W','E','R','T','Y','U','I','O','P','[',']','\\','A','S','D','F','G','H','J','K','L',';','\'','Z','X','C','V','B','N','M',',','.','/'};
using namespace std;
int main()
{
char ch,flag;
while((ch=getchar())!=EOF)
{
flag=1;
for(int i=1;ch1[i]!=0;i++)
{
if(ch1[i]==ch)
{
flag=0;
cout<<ch1[i-1];
}
}
if(flag==1)
cout<<ch;
}
return 0;
}
改进后:
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
int i,ch;
char s[]="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
while((ch=getchar())!=EOF)
{
for(i=1;s[i]&&s[i]!=ch;i++);//先找再判
if(s[i]) printf("%c",s[i-1]);
else putchar(ch);
}
return 0;
}
浙公网安备 33010602011771号