458 - The Decoder

来源:互联网 发布:中班美工交通标志教案 编辑:程序博客网 时间:2024/06/06 02:11

Write a complete program that will correctly decode a set of characters into a valid message. Your program should read a given file of a simple coded set of characters and print the exact message that the characters contain. The code key for this simple coding is a one for one character substitution based upon a single arithmetic manipulation of the printable portion of the ASCII character set.

Input and Output

For example: with the input file that contains:

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu51PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu51KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

your program should print the message:

*CDC is the trademark of the Control Data Corporation.*IBM is a trademark of the International Business Machine Corporation.*DEC is the trademark of the Digital Equipment Corporation.

Your program should accept all sets of characters that use the same encoding scheme and should print the actual message of each set of characters.

Sample Input

1JKJ'pz'{ol'{yhklthyr'vm'{ol'Jvu{yvs'Kh{h'Jvywvyh{pvu51PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu51KLJ'pz'{ol'{yhklthyr'vm'{ol'Kpnp{hs'Lx|pwtlu{'Jvywvyh{pvu5

Sample Output

*CDC is the trademark of the Control Data Corporation.*IBM is a trademark of the International Business Machine Corporation.*DEC is the trademark of the Digital Equipment Corporation.

解题思路:

本题知道破译字符串的方法即可,通过样例中的输入输出,我们可以很快的发现破译前和破译后字符的ASCII码相差7。对于字符串的输入输出,scanf和printf的效率要高于cin和cout,且本题有多组输入输出,所以该题我用了C语言的输入输出和cstring头文件。

AC代码:

#include <iostream>#include<cstdio>#include<cstring>using namespace std;int main(){    char message[1000];    int length;    while( scanf("%s", message) != EOF )//输入需要破译的字符串    {        length=strlen( message );//求得字符串长度        for(int i = 0; i < length; i++)        {            message[i] -= 7;//每个字符ASCII码减7便能破译出原始字符        }        printf("%s\n", message);//输出破译后的字符串    }    return 0;}

0 0
原创粉丝点击