标题:一道题目急需大家帮忙 !!
只看楼主
狮子
Rank: 2
等 级:论坛游民
帖 子:25
专家分:13
注 册:2009-5-23
结帖率:100%
已结贴  问题点数:50 回复次数:16 
一道题目急需大家帮忙 !!
题目是全英的,大概意思是将一行字符串加密
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.
如果输入以下内容:
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
就有以下输出显示:
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
 各位高手帮帮忙吧~~
搜索更多相关主题的帖子: one situation general captain 
2009-07-21 15:11
cantin
Rank: 1
等 级:新手上路
帖 子:1
专家分:2
注 册:2009-7-21
得分:2 
应该是用FOR语句
先输入一个字母
如果是A到E的话,加上21
其余的减5
2009-07-21 16:09
soky
Rank: 4
等 级:业余侠客
帖 子:126
专家分:228
注 册:2009-7-13
得分:0 
应该是一道密码题,英文不好,看不太懂
2009-07-21 16:21
prankmoon
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:161
专家分:921
注 册:2009-7-21
得分:10 
这个是凯撒密码的解密部分。我这里写出另外一种比较笨的使用求余的方法:

程序代码:
/**

 *      file_name:      caesar_de.c

 *    description:

 *

 *        version:      1.0

 *        created:      16:38 2009-7-21

 *       revision:      none 

 *       compiler:      VC6.0

 *

 *         author:      prankmoon@*        company:

 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXLEN      1000

int caesar_de(const char *in, int in_len, char *out)
{
    int i;
    char c;
    
    /* basic check */
    assert((in != NULL) && (in_len > 0) && (out != NULL));
    
    /* core */
    for (i=0; i<in_len; i++)
    {
        if ((in[i] >= 'A') && (in[i] <= 'Z'))
        {
            c = in[i] - 'A' - 5;
            out[i] = (c >= 0) ? (c % 26 + 'A') : (c + 26 + 'A');
        }
        else
        {
            out[i] = in[i];
        }
    }
    
    out[in_len] = '\0';
    
    return in_len;
}

int main(int argc, char *argv[])
{
    char ciphertext[MAXLEN];
    char plaintext[MAXLEN];
    
    while (1)
    {
        printf("enter the ciphertext to decrypt:\n\t");
        memset(ciphertext, 0, MAXLEN);
        memset(plaintext, 0, MAXLEN);
        
        gets(ciphertext);
        
        caesar_de(ciphertext, strlen(ciphertext), plaintext);
        
        printf("your plaintext is:\n\t%s\n\n", plaintext);
    }
    
    return 0;
}


[[it] 本帖最后由 prankmoon 于 2009-7-21 16:46 编辑 [/it]]
2009-07-21 16:41
soky
Rank: 4
等 级:业余侠客
帖 子:126
专家分:228
注 册:2009-7-13
得分:5 
#include<stdio.h>
int main()
{
    char ch;
    for(ch=getchar();ch!='\n';ch=getchar())
    {
        if(ch>=65&&ch<=69||ch>=97&&ch<=101)
        printf("%c",ch+21);
        else if(ch>=70&&ch<=90||ch>=102&&ch<=122)
        printf("%c",ch-5);
        else
        printf("%c",ch);
    }
}
简单写了下,其他功能自己加。
ps:像这种题目,自己想一下就应该明白了,多想多测试,别怕浪费时间。

[[it] 本帖最后由 soky 于 2009-7-21 19:46 编辑 [/it]]
2009-07-21 16:43
狮子
Rank: 2
等 级:论坛游民
帖 子:25
专家分:13
注 册:2009-5-23
得分:0 
我自己的程序,大家看看行不行~~
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    char star[11],end[11],meg[100];
    int i;
    do
    {
        
        gets(star);
        if(strcmp(star,"ENDOFINPUT")==0)
            break;
        gets(meg);
        gets(end);
        for(i=0;i<strlen(meg);i++)
        {
            if(meg[i]>='A'&&meg[i]<='E')
                printf("%c",(90-4+(meg[i]-'A')));
            else if(meg[i]>='F'&&meg[i]<='Z')
                printf("%c",meg[i]-5);
            else printf("%c",meg[i]);
            
        }
        printf("\n");
        
        
    }while(1);
    return 0;
}
2009-07-21 19:35
soky
Rank: 4
等 级:业余侠客
帖 子:126
专家分:228
注 册:2009-7-13
得分:0 
老师说,一般编程不用do ……while,所以学的有点差,看不太懂。
2009-07-21 19:46
狮子
Rank: 2
等 级:论坛游民
帖 子:25
专家分:13
注 册:2009-5-23
得分:0 
do-while 很好用的哦~
2009-07-22 15:18
狮子
Rank: 2
等 级:论坛游民
帖 子:25
专家分:13
注 册:2009-5-23
得分:0 
回复 4楼 prankmoon
很好啊~不过题目说是当输入ENDOFINPUT时就结束循环哦,所以好像不符合要求哦~
不过~真的很好啦!(*^__^*) 嘻嘻……
2009-07-22 15:25
NoSoul
Rank: 9Rank: 9Rank: 9
来 自:沈阳化工大学
等 级:蜘蛛侠
帖 子:283
专家分:1010
注 册:2009-6-6
得分:20 
回复 楼主 狮子
#include<stdio.h>
#include<string.h>
void decipher(char message[]);
int main()
{
    char message[201];
    gets(message);
    while(strcmp(message,"START")==0){
        decipher(message);
        printf("%s\n",message);
        gets(message);
    }
    return 0;
}
void decipher(char message[])
{
    char plain[27]="VWXYZABCDEFGHIJKLMNOPQRSTU";  
    char cipherEnd[201];
    int i,cipherlen;
    gets(message);
    cipherlen=strlen(message);
    for(i=0;i<cipherlen;i++)
        if(message[i]>='A'&&message[i]<='Z')
            message[i]=plain[message[i]-'A'];
    gets(cipherEnd);

}

我想伸手拉近點,竟觸不到那邊,就欠一點點,但這一點點...卻好遠
2009-07-22 18:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-280043-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.050931 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved