简单密码

GitHub跳转原题关系图返回列表

对大写密文字母在 26 个位置上循环左移五位,保留非字母字符。

OJ: noi_openjudge

题目 ID: ch0107-10

难度:入门

标签:字符串模拟python

日期: 2026-07-30 23:01

题意

凯撒密文将明文大写字母后移五位。给定密文,恢复明文;非字母字符不变。

思路

把大写字母映射到 002525。解密相当于减去 55% 26 处理 A 前面的循环,再转回字符。非大写字母原样返回。

代码

Python代码

python
def decrypt(character: str) -> str:
    if "A" <= character <= "Z":
        return chr((ord(character) - ord("A") - 5) % 26 + ord("A"))
    return character


print("".join(decrypt(character) for character in input()))

C++代码

cpp
#include <cstdio>
#include <cstring>
#define maxn 500

int n;
char str[maxn];
int idx=1;

char mi[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char ming[] = "VWXYZABCDEFGHIJKLMNOPQRSTU";

int main(){
    char t;
    int i;
    while(1){
        scanf("%c",&t);
        if( t == '\n' || t == '\r' || t == EOF)
            break;
        str[idx] = t;
        idx++;
    }
    idx--;

    for(i=1;i<=idx;i++){
        char c = str[i];
        if( c >= 'A' && c <= 'Z'){
            printf("%c",ming[ c-'A']);
        }
        else
            printf("%c",c);
    }
    return 0;
}

复杂度

时间复杂度和输出空间均为 O(n)O(n)

总结

循环字母表的平移可统一为“转下标、取模、转回字符”。