设为首页 加入收藏

TOP

HDU1048
2017-10-12 17:39:19 】 浏览:8356
Tags:HDU1048
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
 
 
 1 #include"stdio.h"
 2 #include"string.h"
 3 int main(void)
 4 {
 5     char fr[10]="START",end[10]="END",main[1000];
 6     int len,i;
 7     while(scanf("%s",fr)!=EOF&&strcmp(fr,"ENDOFINPUT")!=0)
 8     {
 9         getchar();  //输入段
10         gets(main);
11         scanf("%s",end);
12         
13         len=strlen(main);  //处理段
14         for(i=0;i<len;i++)
15         {
16             if(main[i]>'Z' || main[i]<'A')        continue;    
17             if(main[i]>'E')                        main[i]=main[i]-5;
18             else                                main[i]='Z'-(5-main[i]+'A')+1;
19         }    
20         for(i=0;i<len;i++)  //输出段,puts()应该也可以
21             printf("%c",main[i]);
22         printf("\n");
23     }
24     return 0;
25 }

 

 

这段代码我写了两个小时,不要笑我(很不开心),最开始的算法完全是错的,因为我的思路里,把 "END" 忽略了,发现的时候就比较麻烦了,而且关于 puts() , %s的语法在换行方面的处理不太清楚,C语言只是一种语言,但其他语言在换行字符上的纠结,我觉的以后在我的学习之路上也应该留意下。

在字符组的使用里,尤其要注意字符组长度,一定要略大于需要长度,而且要注意对 '\0' 结束符的处理(这点是最烦人的)。

至于 1048 的解题思路就不细说了,真的是一道水题,看代码吧

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇hdu1037 下一篇调试技巧总结

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目