码迷,mamicode.com
首页 > 其他好文 > 详细

UVa10082 WERTYU

时间:2015-02-19 18:38:15      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

10082 WERTYU
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.

技术分享

这道题貌似在tyvj上见过,当时认为要用一连串的if或者是swich,看了白书之后才发现利用常亮数组可以很好地解决这道题。

一个需要注意的地方就是’\’需要用转移字符’\\’来表示。

废话不多说了,直接上代码:

#include <stdio.h>
char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;‘ZXCVBNM,./";/*pay attenrion to \\ */
int main()
{
  int i, c;
  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;
}

话说Dev-C++的Astyle真是恐怖……好好的代码顿时没有美感了,强迫症的我还是仔细的修改了一下。

putchar()输出单个字符是相当方便的,个人认为手打scanf()的速度比putchar()慢多了(可能是我手速慢的原因)。

这个代码里在数组里寻找单个元素的写法也是值得学习的。

UVa10082 WERTYU

标签:

原文地址:http://www.cnblogs.com/cuichen/p/4296204.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!