HDU 1048 The Hardest Problem Ever
原题大意:
给出相应的密码本,将密文字母按照所给密码本列出的对应关系解密,字母均为大写,对于非大写字母则同密文保持一致,即不用翻译原样输出。
密码本: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
输入:
每个密文由3部分组成:起始行---标记为“START”;密文Cipher message;结束行---标记为“END”。
输出:
对于每个密文,将其按照密码本破译为明文。
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 4 int main() 5 { 6 char cipher[1024]; 7 int i; 8 while(1) 9 { 10 gets(cipher); 11 if(strcmp("ENDOFINPUT",cipher) == 0) 12 { 13 break; 14 } 15 if(strcmp(cipher,"START") == 0 || strcmp(cipher,"END") == 0) 16 { 17 continue; 18 } 19 for(i = 0;i < strlen(cipher); ++i) 20 { 21 if(cipher[i] >= 'F' && cipher[i] <= 'Z') 22 { 23 cipher[i] -= 5; 24 } 25 else if(cipher[i] >= 'A' && cipher[i] <= 'E') 26 { 27 cipher[i] += 21; 28 } 29 printf("%c",cipher[i]); 30 } 31 printf("\n"); 32 } 33 return 0; 34 }
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
7716920 | 2013-03-08 23:25:34 | Accepted | 1048 | 0MS | 228K | 540 B | GCC | whseay |
解法二:
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 char alpha[]="VWXYZABCDEFGHIJKLMNOPQRSTU"; 6 char str[1024]; 7 int i,len; 8 while(gets(str)) 9 { 10 if(strcmp(str, "ENDOFINPUT") == 0) 11 { 12 break; 13 } 14 if(strcmp(str, "START") != 0 && strcmp(str, "END") != 0) 15 { 16 len = strlen(str); 17 for(i = 0;i < len; i++) 18 { 19 if(str[i] >= 'A' && str[i] <= 'Z') 20 printf("%c",alpha[str[i] - 'A']); 21 else 22 printf("%c",str[i]); 23 } 24 printf("\n"); 25 } 26 } 27 return 0; 28 }
Run ID | Submit Time | Judge Status | Pro.ID | Exe.Time | Exe.Memory | Code Len. | Language | Author |
7717337 | 2013-03-09 00:48:49 | Accepted | 1048 | 0MS | 228K | 628 B | GCC | whseay |