The Hardest Problem Ever (有史以来最难的问题)
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
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
代码:
题意:密文对应的纯文本是密文向左移动5个位置的英文,其中A到E对应的是字母表中最后5个字母。注意:只移动字母。
#include<stdio.h>
#include<string.h>//字符串函数的头文件
int main(){
char s1[20],s2[20],s[215],ch;
int i;
while(1){
scanf("%s",s1);//输入START
if(strcmp(s1,"ENDOFINPUT")==0)//结束循环的标志
break;
getchar();//注意吸收是s1中的回车字符,否则ch会把回车吸收,结束下一循环,导致错误
i=0;
while((ch=getchar())!='\n'){
if(ch>='A'&&ch<'F'){
s[i]=ch-'A'+'V';
i++;//前五个字母对应字母表中的最后五个字母
}
else if(ch>='F'&&ch<='Z'){
s[i]=ch-5;//其余字母对应的是原字母减5的英文
i++;}
else{
s[i]=ch;//非字母保持不变
i++;
}
}
scanf("%s",s2);//输入END
for(int k=0;k<i;k++)//每组的输出
printf("%c",s[k]);
printf("\n");
}
return 0;
}