程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> HDU1048,1048幻影凶間

HDU1048,1048幻影凶間

編輯:關於C語言

HDU1048,1048幻影凶間


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 的解題思路就不細說了,真的是一道水題,看代碼吧

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved