HDU 1048

HDU 1048

The Hardest Problem Ever

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26692 Accepted Submission(s): 12508

Problem Description
Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked.
You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:

Cipher text
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Plain text
V W X Y Z A B C D E F G H I J K L M N O P Q R S T U

Only letters are shifted in this cipher. Any non-alphabetical character should remain the same, and all alphabetical characters will be upper case.

Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets. All characters will be uppercase.

A single data set has 3 components:

Start line - A single line, “START”

Cipher message - A single line containing from one to two hundred characters, inclusive, comprising a single message from Caesar

End line - A single line, “END”

Following the final data set will be a single line, “ENDOFINPUT”.

Output
For each data set, there will be exactly one line of output. This is the original message by Caesar.

Sample Input
START
NS BFW, JAJSYX TK NRUTWYFSHJ FWJ YMJ WJXZQY TK YWNANFQ HFZXJX
END
START
N BTZQI WFYMJW GJ KNWXY NS F QNYYQJ NGJWNFS ANQQFLJ YMFS XJHTSI NS WTRJ
END
START
IFSLJW PSTBX KZQQ BJQQ YMFY HFJXFW NX RTWJ IFSLJWTZX YMFS MJ
END
ENDOFINPUT

Sample Output
IN WAR, EVENTS OF IMPORTANCE ARE THE RESULT OF TRIVIAL CAUSES
I WOULD RATHER BE FIRST IN A LITTLE IBERIAN VILLAGE THAN SECOND IN ROME
DANGER KNOWS FULL WELL THAT CAESAR IS MORE DANGEROUS THAN HE

Source
South Central USA 2002

试题分析:
这是一道考查字符串处理的题目,题目本身并不难以处理处理,可以使用很多方法,包括使用指针来处理字符串来增加代码执行效率,但是多数算法竞赛采用的都是黑箱测试,即只考察使用程序解决问题的能力,而不关心使用什么方法,只要可以解决问题就好了。这道题我做了很久,出了一些错误,我相信这些地方很多人也会犯同样的错误,有必要和大家分享一下。
1.需要使用gets()函数输入字符串,因为我们写题目时输入经常使用scanf()函数,scanf函数可以输入字符串,但是一旦字符串中出现空格(’ ‘)就会把接下来的值输入到另外一个变量中,导致结果出错,如果这里使用了scanf(“%s”,ch)输入,就会得到多个ch的值,导致每个词语都占一行;
2.if(ch[i]>=’F’&&ch[i]<=’Z’) ch[i]=ch[i]-5;
else if(ch[i]>=’A’&&ch[i]<=’E’) ch[i]=ch[i]+21;
这个else不能删掉,否者ch[i]的值会互相影响,出现结果与答案不符的现象;
3.使用strlen函数计算数组字符串长度时最好不要放到for循环中,否则会出现警告;
4.在不熟悉sizeof运算符时,最好不要使用,否者很容易出错;
5.最后就是写这道题英语一定要学好,否者根本看不懂,除非借助一些翻译工具。

代码如下:
#include<stdio.h>
#include<string.h>
int main()
{
    int i,k;
    char a[]="START",b[]="END",c[]="ENDOFINPUT",ch[200];
    while(1)
    {
        gets(ch);
        if(strcmp(ch,c)==0) break;
        if(strcmp(ch,a)==0||strcmp(ch,b)==0) continue;
        k=strlen(ch);
        for(i=0;i<k;i++)
        {
            if(ch[i]>='F'&&ch[i]<='Z') ch[i]=ch[i]-5;
            else if(ch[i]>='A'&&ch[i]<='E') ch[i]=ch[i]+21;
        }
        printf("%s\n",ch);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值