[解题报告]458 The Decoder

 

 The Decoder 

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{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'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{pvu5
1PIT'pz'h'{yhklthyr'vm'{ol'Pu{lyuh{pvuhs'I|zpulzz'Thjopul'Jvywvyh{pvu5
1KLJ'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.



看完题就傻了,WTF,仔细再看了一遍才发现简单到爆

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.

这个句子就是关键句,关键词:一一对应,算法规则只有一种,ASCII表

这说明密文翻译是基于ASCII表的,而且算法只有一个

所以一开始写了一个简单程序找到了规律

#include<stdio.h>
void main()
{
    int i;
    i='1'-'*';
    printf("%d",i);
}

运行结果为7

这说明密文中每一个字符所对应的ASCII值减去7就是原文中每一个字符所对应的ASCII值

要注意如果没有赋初值就整体字符组打印,会出现乱码

下面这个程序不知道为毛判题系统一直“- In queue -”了六分钟(。。。)然后告诉我提交错误

#include<stdio.h>
#include<string.h>
void main()
{
    int i,n;
    char miwen[100000];
    while(gets(miwen)!=EOF)
    {
        n=strlen(miwen);
        for(i=0;i<n;i++)
        {
            printf("%c",miwen[i]-7);
        }
        printf("\n");
    }
}

而这个就AC

#include<stdio.h>
#include<string.h>
void main()
{
    int i,n;
    char miwen[100000];
    while(gets(miwen)!=NULL)
    {
        n=strlen(miwen);
        for(i=0;i<n;i++)
        {
            printf("%c",miwen[i]-7);
        }
        printf("\n");
    }
}

看来得研究一下NULL和EOF的区别。。。。

原文地址:https://www.cnblogs.com/TheLaughingMan/p/2892077.html