A common typing error is to place the hands on the keyboard one row to the right of the correct position. So ‘Q’ is typed as ‘W’ and ‘J’ is typed as ‘K’ and so 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 (except Q, 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 Input
O S, GOMR YPFSU/
Sample Output
I AM FINE TODAY.
题意:
把手放在键盘上时,稍不注意就会往右错一 位。这样,输入Q会变成输入W,输入J会变成输 入K等。
输入一个错位后敲出的字符串(所有字母均 大写),输出打字员本来想打出的句子。输入保 证合法,即一定是错位之后的字符串。例如输入中不会出现大写字母A。
AC代码1:(比较复杂的)
#include<string.h>
{
char c;
while((c = getchar())!=EOF)
{
switch(c)
{
case'W':
printf("Q");
break;
case'E':
printf("W");
break;
case'R':
printf("E");
break;
case'T':
printf("R");
break;
case'Y':
printf("T");
break;
case'U':
printf("Y");
break;
case'I':
printf("U");
break;
case'O':
printf("I");
break;
case'P':
printf("O");
break;
case'[':
printf("P");
break;
case']':
printf("[");
break;
case'\\':
printf("]");
break;
case'S':
printf("A");
break;
case'D':
printf("S");
break;
case'F':
printf("D");
break;
case'G':
printf("F");
break;
case'H':
printf("G");
break;
case'J':
printf("H");
break;
case'K':
printf("J");
break;
case'L':
printf("K");
break;
case';':
printf("L");
break;
case'\'':
printf(";");
break;
case'X':
printf("Z");
break;
case'C':
printf("X");
break;
case'V':
printf("C");
break;
case'B':
printf("V");
break;
case'N':
printf("B");
break;
case'M':
printf("N");
break;
case',':
printf("M");
break;
case'.':
printf(",");
break;
case'/':
printf(".");
break;
case'1':
printf("`");
break;
case'2':
printf("1");
break;
case'3':
printf("2");
break;
case'4':
printf("3");
break;
case'5':
printf("4");
break;
case'6':
printf("5");
break;
case'7':
printf("6");
break;
case'8':
printf("7");
break;
case'9':
printf("8");
break;
case'0':
printf("9");
break;
case'-':
printf("0");
break;
case'=':
printf("-");
break;
default :
printf("%c",c);
break;
}
}
}
#include<string.h>
int main()
{
char c;
char str[]="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;''ZXCVBNM,./";
while((c=getchar())!=EOF)
{
int i,flag=1;
for(i=1;str[i];i++)
{
if(c==str[i])
{
printf("%c",str[i-1]);
flag=0;
break;
}
}
}
}
本文介绍了一种解决键盘输入错位问题的算法,通过将输入的字符替换为QWERTY键盘布局上左侧相邻的字符来还原正确的文本。文章提供了两种实现方式,一种采用复杂的逐字符判断,另一种则利用字符串映射简化了处理流程。
447

被折叠的 条评论
为什么被折叠?



