【OI】WERTYU UVa 10082

题目:

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.


此题的关键就在于如何确定给定字符在键盘上的左边一个字符是什么。可以用30个if但是太烦了。这里涉及到一个常量数组。将键盘上的字符按照行顺序写进一个数组,这样用数组的下标可以访问左边的字符。

char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

但是这给循环搜索造成了麻烦。字符数组的最后一个一定是空字符,用这个判断数组结束。

for (i=1;s[i];i++);  //这里不需要考虑i=0,这种情况被题设排除

于是顺便将条件一起解决了

for (i=1;s[i] && c == s[i];i++);

最后输出,如果i不是s数组的最后一个,就直接替换为s[i-1],否则就输出c(就是空格了)

if(s[i])  putchar(s[i-1]);

else   putchar(c);

照例,本题使用逐个读取。

while ((c = getchar()) != EOF){…………}  //考虑到可能有空格的出现

解:

 

#include <stdio.h>

int main()
{
    int c,i;
    char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
    while ((c = getchar()) != EOF)
    {
        for (i=1;s[i] && s[i] != c;i++);
        if (s[i])    putchar(s[i-1]);
        else putchar(c);
    }
    return 0;
}

 

https://vjudge.net/problem/UVA-10082(UVa太慢)

 

posted @ 2019-07-23 18:41  AlexanderZ.Tang  阅读(185)  评论(0)    收藏  举报