zoukankan      html  css  js  c++  java
  • UVA 10082 WERTYU

    Problem C: 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 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. 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/
    

    Output for Sample Input

    I AM FINE TODAY.

    题意:输入一窜字符,然后输出每个字符在键盘上对应位置的前一位的字符,如输入W则输出Q,注意全部是大小写!其实就是破解密码类的问题。
    分析:将题目中要求输入的键盘上是字符及其前面的字符存放在一个数组str2中,然后将输入的字符(在数组str1中)与数组str2中的字符相比较,找到相同的字符后输出这个字符在str2数组中前一位的字符。注意:特殊字符要用转义字符形式表示!细节见程序注释!
    AC源代码(C语言):
    #include<stdio.h>
    int main()
    {
      int i,j,a,b,m;
      char str1[1000];
      char str2[]={'`','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',',','.','/'};
      b=sizeof(str2);
      while(scanf("%c",&str1[0])==1)
       {
         for(i=1;str1[i-1]!='\n';i++)
            scanf("%c",&str1[i]);
         a=i-1;//不要写成a=sizeof(str1)的形式,否则后面会有乱码
         for(i=0;i<a;i++)
           {
             for(j=0;j<b;j++)
               {
                 if(str1[i]==str2[j])
                   str1[i]=str2[j-1];
               }
           }
         for(i=0;i<a;i++)
            printf("%c",str1[i]);//用printf("%s",str1);是自动输出空行,因为这样写就是把str1当成了字符串而不是字符,即在原来字符的最后加上了'\0'!所以不用再单独输出一个空行!!!!!但是输出的时候为什么会有乱码 ???????? 
         printf("\n");   //位置放对!!不是return上面!
       }
      return 0;
    }

    另外附上我一开始写的代码(错误的!%>_<%),主要是上面的注释。一个假期没写,手生了许多,好多浅显的问题也犯错,别笑话哈!

     1 #include<stdio.h>
     2 int main()
     3 {
     4   int i,j,a,b,m;
     5   char str1[1000];
     6   char str2[]={'`','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',',','.','/'};
     7   b=sizeof(str2);  //str2中的特殊字符:' 和\要用转义字符表示!!!!!!!!!! 
     8   while(scanf("%s",&str1)==1) //这样输入的时候字符数组不接受空格!!!!!!!!!!!!
     9    { 
    10      a=sizeof(str1);         //不要写成a=sizeof(str1)的形式,否则后面会有乱码,因为sizeof计算的是内存中为str1分配的空间,而不是str1中实际用到的字节的长度!!!!!!!!!!!!!!!!! 
    11      for(i=0;i<a;i++)
    12        {
    13          if(str1[i]==' ') continue;        //可以省略。    
    14          for(j=0;j<b;j++)
    15            {
    16              if(str1[i]==str2[j])
    17                str1[i]=str2[j-1];
    18            }
    19        }
    20      printf("%s",str1); //用printf("%s",str1);是自动输出空行,因为这样写就是把str1当成了字符串而不是字符,即在原来字符的最后加上了'\0'!所以不用再单独输出一个空行!!!!!但是输出的时候会有乱码,因为它输出的是str1[1000],而不是实际的str1!    
    21    }
    22   printf("\n");   //位置不对,放这个位置表示在程序通过出口之前输出一个空格!而实际要求的效果是没输入一个回车就要输出一个空行,即每一个案例都要带有一个空行!!!!!!!!! 
    23   return 0;
    24 }  
    25 // 为什么有的程序在DEV-C++和codeblocks上运行的时候正确,而在uva上用ANSI C提交的时候显示    “Compilation error“,但是用C++提交的时候就ACl了!!!!!WHY???答案很简单,因为C语言中的注释只支持“/*”的格式,不支持“//”双杠是格式!坑啊....... 

    2013-03-14

  • 相关阅读:
    在VS2010下,用C语言编写pthread多线程程序的配置
    java帮助文档系列JDK1.5 JDK1.6 JDK1.7官方中英完整版下载
    瑜伽练习day02----适合练习瑜伽时听的歌曲
    瑜伽练习day01----瑜伽练习的好处,坏处
    抛出错误Debug Assertion Failed!
    stringstream的基本用法
    AWS中S3的Bucket桶访问策略
    AWS中SQS的几项使用注意
    AWS在.Net中连接数据库时发生认证错误的解决办法
    .Net捕捉配置或程序错误
  • 原文地址:https://www.cnblogs.com/fjutacm/p/2960472.html
Copyright © 2011-2022 走看看