首页 > 试题广场 >

简单密码

[编程题]简单密码
  • 热度指数:19624 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
Julius Caesar曾经使用过一种很简单的密码。 对于明文中的每个字符,将它用它字母表中后5位对应的字符来代替,这样就得到了密文。 比如字符A用F来代替。如下是密文和明文中字符的对应关系。 密文 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 明文 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 你的任务是对给定的密文进行解密得到明文。 你需要注意的是,密文中出现的字母都是大写字母。密文中也包括非字母的字符,对这些字符不用进行解码。

输入描述:
输入中的测试数据不超过100组。每组数据都有如下的形式,而且各组测试数据之间没有空白的行。
一组测试数据包括三部分:
1.    起始行 - 一行,包括字符串 "START" 
2.    密文 - 一行,给出密文,密文不为空,而且其中的字符数不超过200
3.    结束行 - 一行,包括字符串 "END" 
在最后一组测试数据之后有一行,包括字符串 "ENDOFINPUT"。


输出描述:
对每组数据,都有一行输出,给出密文对应的明文。
示例1

输入

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
a = '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'.split()
b = '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'.split()
table = dict(zip(a, b))

try:
    while 1:
        s = raw_input()
        if s == 'ENDOFINPUT':
            break
        if s == 'START' or s == 'END':
            continue
        output = ''
        for i in s:
            if i in table:
                output += table[i]
            else:
                output += i
        print output
except:
    pass

发表于 2016-12-29 21:49:56 回复(0)
我这个开始也不过,但是把while里面的cin变成getline以后就通过了
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
    string start;
    while(getline(cin,start)){
        if(start=="ENDOFINPUT")
            break;
        if(start=="START"){
            char ch[200];
            string str;
            getline(cin,str);
            strcpy(ch,str.c_str());
            for(int i=0;i<str.length();i++){
                if('A'<=ch[i]&&ch[i]<='Z'){
                    switch(ch[i]){
                        case 'A': ch[i]='V'; break;
                        case 'B': ch[i]='W'; break;
                        case 'C': ch[i]='X'; break;
                        case 'D': ch[i]='Y'; break;
                        case 'E': ch[i]='Z'; break;
                        default: ch[i]-=5;
                    }
                }
            }
            for(int i=0;i<str.length();i++)
                cout<<ch[i];
            getline(cin,str);
            cout<<endl;
        }
    }
}

发表于 2019-02-09 20:23:52 回复(4)

python solution,通过测试:

password="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
text="VWXYZABCDEFGHIJKLMNOPQRSTU"
while True:
    try:
        a=input()
        if a=="START":
            string=input()
            res=""
            for i in string:
                if i.isupper():
                    res+=text[password.index(i)]
                else:
                    res+=i
            print(res)
    except:
        break
编辑于 2017-10-16 17:43:22 回复(0)
写个垃圾题解
using namespace std;
#include <iostream>
#include <cstring>
int main(){
    string mess;
    while(getline(cin, mess) and mess != "ENDOFINPUT"){
        if(mess != "START" and mess != "END"){
            for(int i(0);i<mess.size();++i){
                if(mess[i] <= 'Z' and mess[i] >= 'A'){
                    mess[i] = (mess[i] - 5 - 'A' + 26)%26 + 'A';
                }
            }
            cout<<mess<<endl;
        }
    }
    return 0;
}


发表于 2021-06-18 10:23:30 回复(0)
#include <stdio.h>
(737)#include <string.h>
#define SIZE 2001
int main()
{
    char IN[3][SIZE];
    int j,k;
    while(gets(IN[0])!=NULL)
    {
        if(strcmp(IN[0],"ENDOFINPUT")==0)
        {
            break;
        }
        gets(IN[1]);
        for(j=0;IN[1][j]!='\0';j++)
        {
            if(IN[1][j]>='A'&&IN[1][j]<='Z')
            {
                IN[1][j]=(IN[1][j]-'A'+21)%26+'A';
            }
        }
        gets(IN[2]);
        puts(IN[1]);
    }
    return 0;
}


发表于 2020-04-16 12:37:58 回复(1)
#include<stdio.h>
(737)#include<string.h>
int main()
{
    char a[20],b[100],c[20];
    while(gets(a)!=NULL)
    {
        if(strcmp(a,"ENDOFINPUT")==0) break;
        gets(b);
        for(int i=0;i<strlen(b);i++)
            if(b[i]>='A'&&b[i]<='Z')
            {
                b[i]-=5;
                if(b[i]<'A')
                    b[i]=b[i]+26;
            }
        gets(c);
        printf("%s\n",b);
    }
	return 0;
}

发表于 2020-04-07 15:23:13 回复(0)
while True:
    try:
        start = input()
        if start == 'ENDOFINPUT':
            break
        conversion = 'VWXYZABCDEFGHIJKLMNOPQRSTU'
        cipherText = input()
        input()
        clearText = []
        for i in cipherText:
            if i.isupper():
                clearText.append(conversion[ord(i)-ord('A')])
            else:
                clearText.append(i)
        print("".join(clearText))

    except Exception:
        break
编辑于 2018-09-28 16:49:28 回复(0)
# include <stdio.h>
#include<string.h>
int main() {
    char str[201];
    char start[] = "START", final[] = "ENDOFINPUT";
    for (int count = 0; count < 100; count++) {
        gets(str);
        if (strcmp(start, str) == 0) { //start
            gets(str);
            for (unsigned i = 0; i < strlen(str); i++) {
                if (str[i] <= 'Z' && str[i] >= 'A') {
                    if (str[i] >= 'F') {
                        str[i] -= 5;
                    }
                    else {
                        str[i] += 21;
                    }
                }
            }
            printf("%s\n", str);
        }
        else if (strcmp(final, str) == 0) {
            break; //final
        }
    };
    return 0;
}

编辑于 2018-02-27 22:30:16 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String input = scanner.nextLine();
            if (input.equals("ENDOFINPUT"))
                break;
            if (input.equals("START"))
                continue;
            else if (input.equals("END"))
                continue;
            else {
                for (char c : input.toCharArray()) {
                    if (Character.isUpperCase(c))
                        System.out.print((char) ('A' + (c - 'A' - 5 + 26) % 26));
                    else
                        System.out.print(c);
                }
                System.out.println();
            }
        }
        scanner.close();
    }

}



发表于 2018-01-21 22:00:11 回复(0)
//用getline函数取得整行输入再进行操作
#include<iostream>
using namespace std;
#include<string>

int main() {
	string s;
	while (getline(cin, s) && s != "ENDOFINPUT") {
		getline(cin, s);
		for (int i = 0;i<s.size();i++) {
			if (s[i] >= 'F' && s[i] <= 'Z')
				s[i] -= 5;
			else if (s[i] >= 'A' && s[i] <= 'E')
				s[i] += 'V' - 'A';
		}
		cout << s << endl;
		getline(cin, s);
	}
}

发表于 2017-07-09 17:10:47 回复(0)
被格式搞死了,原来每一行的最后并不保证没有空格存在的,所以只能整行转化,不能一个个单词的转换。
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

string change(string s)
{
	string res;
	
	for (auto c : s)
	{
		if (c >= 'A'&&c <= 'Z')
		{
			if (c == 'A')
				res += 'V';
			else if (c == 'B')
				res += 'W';
			else if (c == 'C')
				res += 'X';
			else if (c == 'D')
				res += 'Y';
			else if (c == 'E')
				res += 'Z';
			else
				res += c - 5;
		}
		else
			res += c;	
	}
	return res;
}
int main()
{
	string s;
	vector<string>  res;
	while (getline(cin,s))
	{
		string str;
		if (s == "START" || s == "END")
			continue;
		else if (s == "ENDOFINPUT")
			break;
		else
		{
			str = change(s);
			res.push_back(str);
		}
	}	
	for (vector<string>::iterator it = res.begin(); it != res.end(); it++)
		cout << *it << endl;
}

发表于 2017-04-13 12:45:12 回复(1)
getline可以通过,cin无法通过;
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;

int main(){
    string a,b;
    while(getline(cin,a)){
       if(a=="ENDOFINPUT"){
           break;
       }
        char str[300];
        gets(str);
        int len=strlen(str);
        for(int i=0;i<len;i++){
            if(str[i]>='A'&&str[i]<='Z'){
                int temp=str[i]-'A';
                if(temp-5<0){
                    temp+=26;
                }
                str[i]='A'+temp-5;
            }
        }
        printf("%s\n",str);
        getline(cin,b);
    }
    return 0;
}
发表于 2019-03-08 09:49:45 回复(0)
#include <stdio.h>
#include <string.h>
#define N 201

char Decode(char c)
{
    if('A'<=c&&c<='Z')
    {
        return 'A'+(c-'A'-5+26)%26;
    }
    else return c;
}

int main()
{
    char str[N];
    char order[N];
    while(gets(order))
    {
        if(strcmp("ENDOFINPUT", order)==0) break;
        gets(str);//读取待处理字符串
        gets(order);//读取"END"
        int len=strlen(str);
        for(int i=0; i<len; i++)
        {
            str[i]=Decode(str[i]);
        }
        puts(str);
    }
    return 0;
}

发表于 2018-02-23 08:44:41 回复(0)
这道题难点在于如何处理输入格式的问题,两种结束符搞的不知道怎么处理了,后来参考了c语言的里getchar()读取多余换行符的思路,利用getline()读掉“END”标识符就好了,只要判断start就行
另外,处理decode的问题,因为这道题只是简单的移位问题,所有直接用取模运算是最简单的

公式:decode后字符='Z'-('Z'-目标字符+5)%26;

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    while(getline(cin,str)){
        if(str=="ENDOFINPUT")break;
        if(str=="START"){
            getline(cin,str);
            for(int i=0;i<str.length();i++){
                if(str[i]>='A'&&str[i]<='Z'){
                    str[i]='Z'-('Z'-str[i]+5)%26;
                }
            }
            cout<<str<<endl;
        }
        getline(cin,str);//读去“END”结尾字符串
    }
    return 0;
}


发表于 2020-02-09 12:33:14 回复(3)
不知道为啥不能通过,求解。
#include<iostream>
#include<string>
using namespace std;
int main(){    
    string s,a,e;
    while(!cin.eof()){
        getline(cin,s);
        if(s=="ENDOFINPUT")break;
        getline(cin,a);
        getline(cin,e);    
        for(int i=0;i<a.size();i++){
            if(a[i]>='F'&&a[i]<='Z')
                cout<<char(int(a[i])-5);
            else if(a[i]>='A'&&a[i]<='E')
                cout<<char(int(a[i])+21);
            else cout<<a[i];        
        
        cout<<"\n";
    }
    return 0;


发表于 2019-01-24 14:36:40 回复(1)
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

//简单密码 
int main(){
	string str;
	while (getline(cin, str)){	//起始行 
		if (str == "ENDOFINPUT") {
			break;
		}
		
		//重新获得密文 
		getline(cin, str);
		
		for (int i = 0; i < str.length(); i++){
			if (str[i] >= 'A' && str[i] <= 'Z') {
				str[i] = (str[i] - 'A' - 5 + 26) % 26 + 'A'; 
			}
		}

		cout << str << endl;
		getline(cin, str);		//结束行 
	}
	return 0;
}

发表于 2023-03-22 16:20:12 回复(0)
#include <iostream>
using namespace std;

struct change {
    int no;
    int to;
};
int main() {
    change c[26];
    string begin, end, str;
    //密文转换,相当于往前面走5个位置
    for (int i = 0; i < 26; i++) {
        c[i].no = i;
        c[i].to = (i - 5);
        if (c[i].to < 0) {
            c[i].to += 26;
        }
    }
    //感觉这里面“START”和“END”好像没什么用,就看看读进来是不是“ENDOFINPUT”
    while (getline(cin, begin)) {
        if (begin == "ENDOFINPUT") {
            break;
        }
        //得到本题真正需要转换的字符串
        getline(cin, str);
        for (int i = 0; i < str.size(); i++) {
            if (isalpha(str[i])) {//如果是字母,则进行密文转换
                str[i] = c[(int)(str[i] - 'A')].to + 'A';
            }
        }
        cout << str << endl;
        getline(cin, end);
    }
    return 0;


}

发表于 2023-03-22 15:49:09 回复(0)
#include <bits/stdc++.h>
using namespace std;

string wen[105];

int main() {
    string s1;
    int zuhao=0;
    while (cin >> s1 && s1 != "ENDOFINPUT") {
        string start,miwen, end;
        getline(cin, start);//上一句只是判断第一行,并不会吃掉他,所以要getline吃掉第一行的start
        getline(cin, miwen); // 读取密文
        getline(cin, end);
        int len = miwen.size();
        string mingwen;
        for (int i = 0; i < len; i++) {
            char temp = miwen[i];
            if (temp >= 'F' && temp <= 'Z') {
                temp -= 5;
                mingwen += temp;
            } else if (temp >= 'A' && temp <= 'E') {
                temp += 21;
                mingwen += temp;
            } else {
                mingwen += temp; // 非字母字符直接添加到明文中
            }
        }
        wen[zuhao++] = mingwen;
    }
    for (int j = 0; j <zuhao; j++) { // 使用 zuhao 控制输出的数量
        cout << wen[j] << endl;
    }
    return 0;
}

编辑于 2024-03-18 14:26:14 回复(0)

int main() {

char start[20];

char str[200];

char end[10];

while(scanf("%s",start)!=EOF){

    getchar();

    if(strcmp(start,"ENDOFINPUT") == 0)

        break;

    gets(str);

    gets(end);

    for(int i=0;i(int)strlen(str);i++){

        if(str[i] >= 'A' && str[i]  'Z'){

            str[i] = str[i] - 5;

            if(str[i]  'A')

                str[i] = str[i] + 26;

        }

        printf("%c",str[i]);

    }

}

return 0;

}

编辑于 2024-03-11 15:07:09 回复(0)
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main(){
    string start, text, end;
    while (getline(cin,start)){
        string res;
        if(start == "ENDOFINPUT"){
            break;
        }
        getline(cin,text);
        getline(cin,end);
        for(int i=0; i<text.size(); ++i){
            if(text[i]>= 'A' && text[i] <= 'Z'){
                if(text[i]<'F'){
                    res.push_back((char(text[i]-5+26)));
                }else{
                    res.push_back(char(text[i]-5));
                }
            }else{
                res.push_back(text[i]);
            }
        }
        cout << res << endl;
    }
}

编辑于 2024-03-03 23:15:19 回复(0)

问题信息

难度:
95条回答 8168浏览

热门推荐

通过挑战的用户

查看代码